【发布时间】:2012-07-13 03:35:30
【问题描述】:
我正在尝试将 n 个文件合并为单个文件。但是我的功能出现了奇怪的行为。该函数在 n 秒内被调用 x 次。假设我有 100 个文件要合并,每秒我调用 5 个文件并合并它。在下一秒,数量翻倍为 10,但从 1-5 是与之前相同的文件,其余的是新文件。它工作正常,但在某些时候,它给出零字节或有时给出正确的大小。
您能帮我找出下面函数中的错误吗?
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是从线程中调用的吗? -
如果您尝试合并 MS Word doc 文件:我认为这不起作用,因为格式不允许。
标签: java io fileinputstream bufferedinputstream bufferedoutputstream