public static void main(String[] args) {
File f1 = new File("D:/JavaTest/1.txt");
File f2 = new File("D:/JavaTest/2.txt");
File f3 = new File("D:/JavaTest/3.txt");
String path = "D:/JavaTest/4.txt";
joinText(path, f1, f2, f3);
}
/**
* 把所有的 file 内容合并到 path 对应的文件中
* @param path
* @param files
*/
public static void joinText(String path, File ...files) {
try {
FileOutputStream fos = new FileOutputStream(path, true);
for (File file : files) {
FileInputStream fis = new FileInputStream(file);
int length = 0;
byte[] bytes = new byte[1024];
while ((length = fis.read(bytes)) != -1) {
fos.write(bytes, 0, length);
}
fis.close();
// \r\n 回车换行
fos.write("\r\n".getBytes());
}
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}