Apache POI 3.8 (당시 최신 안정)은 각 시트 (SXSSF 사용시)에 임시 XML 파일을 생성하지만이 파일을 삭제할 수있는 옵션은 제공하지 않습니다. 이 사실은 600MB의 데이터를 내보낼 경우 600MB의 파일이 2 개 있고 그 중 하나가 삭제 될 때까지 임시 폴더에 있기 때문에이 API를 사용하지 않는 것이 좋습니다.
코드를 살펴보면 SXSSFSheet
클래스의 인스턴스는 SheetDataWriter
입니다. 이 마지막 클래스는 File
인스턴스가 나타내는 임시 파일을 작성하고 유지 관리합니다. 이 개체에 액세스하면 파일을 삭제할 수 있습니다. 이러한 모든 인스턴스는 비공개이므로 이론적으로는 액세스 할 수 없습니다. 그러나 리플렉션을 통해 File
인스턴스에 액세스하여 유용하지만 성가신 파일을 삭제할 수 있습니다!
다음과 같은 방법으로이를 수행 할 수 있습니다. deleteSXSSFTempFiles
을 호출하면 해당 통합 문서의 모든 임시 파일이 삭제됩니다.
/**
* Returns a private attribute of a class
* @param containingClass The class that contains the private attribute to retrieve
* @param fieldToGet The name of the attribute to get
* @return The private attribute
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static Object getPrivateAttribute(Object containingClass, String fieldToGet) throws NoSuchFieldException, IllegalAccessException {
//get the field of the containingClass instance
Field declaredField = containingClass.getClass().getDeclaredField(fieldToGet);
//set it as accessible
declaredField.setAccessible(true);
//access it
Object get = declaredField.get(containingClass);
//return it!
return get;
}
/**
* Deletes all temporary files of the SXSSFWorkbook instance
* @param workbook
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static void deleteSXSSFTempFiles(SXSSFWorkbook workbook) throws NoSuchFieldException, IllegalAccessException {
int numberOfSheets = workbook.getNumberOfSheets();
//iterate through all sheets (each sheet as a temp file)
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheetAt = workbook.getSheetAt(i);
//delete only if the sheet is written by stream
if (sheetAt instanceof SXSSFSheet) {
SheetDataWriter sdw = (SheetDataWriter) getPrivateAttribute(sheetAt, "_writer");
File f = (File) getPrivateAttribute(sdw, "_fd");
try {
f.delete();
} catch (Exception ex) {
//could not delete the file
}
}
}
}
왜 SVN에서 전체 코드베이스를 체크 아웃하고 개미로 그를 구축하지? 아니면 최근 야간 빌드를 잡아? – Gagravarr
@Gagravarr 현재 버전은 3.9-beta1이며, 나는 정말로 costumers를 위해 베타 API를 사용하는 것을 피하고 싶었습니다. –
새로운 기능을 사용하려면 새로운 버전을 사용해야합니다 ... – Gagravarr