在java中连接多个.txt文件 [英] concatenating a number of .txt files in java
本文介绍了在java中连接多个.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多.txt文件。我想连接这些并生成一个文本文件。我怎么在java中做到这一点?
以下是这种情况
I have a number of .txt files. I would like to concatenate those and generate a text file. How would i do it in java? Following is the case
file1.txt file2.txt
串联结果
file3.txt
以下是 file1.txt
的内容 file2.txt
推荐答案
逐个文件读取并写入目标文件。如下所示:
Read file-by-file and write them to target file. Something like the following:
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[n];
for (String file : files) {
InputStream in = new FileInputStream(file);
int b = 0;
while ( (b = in.read(buf)) >= 0) {
out.write(buf, 0, b);
out.flush();
}
}
out.close();
这篇关于在java中连接多个.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文