我只是想看看是否有更好的方法我應該處理這個。我對流的理解是,只要關閉流,封裝在其中的任何流都將被關閉,這就是爲什麼我最終只關閉TarArchiveOutputStream的原因。如果我在rawDir或archiveDir上獲得FileNotFound,我想記錄它,否則我想拋出其他任何東西。與嘗試的Java,異常處理和關閉流,終於
public static void createTarGzOfDirectory(File rawDir, File archiveFile) throws IOException {
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
GzipCompressorOutputStream gzOut = null;
TarArchiveOutputStream tOut = null;
try {
fOut = new FileOutputStream(archiveFile);
bOut = new BufferedOutputStream(fOut);
gzOut = new GzipCompressorOutputStream(bOut);
tOut = new TarArchiveOutputStream(gzOut);
addFileToTarGz(tOut, rawDir, "");
} catch (FileNotFoundException e) {
log.error("File not found: " + e);
} finally {
if(tOut != null) {
tOut.finish();
tOut.close();
}
}
任何其他考慮或改善事情的想法?
爲什麼你認爲你想打電話給'完成'? – bmargulies 2012-04-01 22:40:39