我想合併n文件變成單個文件。但是我的功能有些奇怪。在n秒內調用該功能x次。假設我有100個文件,我將合併,每秒調用5個文件併合並它。並在下一秒的金額是雙重10是,但從1-5是相同的文件,其餘的是新文件。它工作正常,但在某些時候,它給零字節或某個時候給出正確的大小。BufferOutputStream合併文件時寫零字節
你能幫我找出下面我的函數中的錯誤嗎?
public void mergeFile(list<String> fileList, int x) {
int count = 0;
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream("Test.doc"));
for (String file : fileList) {
InputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] buff = new byte[1024];
in.read(buff);
out.write(buff);
in.close();
count++;
if (count == x) {
break;
}
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*,我的英語
是否在線程中調用mergeFile ? – adatapost 2012-07-13 09:42:12
@AVD不,它不是。 – david 2012-07-15 22:44:33
如果您嘗試合併MS Word文檔文件:我不認爲這適用於格式不允許。 – eckes 2014-10-08 23:56:16