编写程序,将两个文件内容合并到一个文件中。
执行代码:
package chengxu1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test {
public static void main(String[] args) {
try {
FileReader fr1 = new FileReader("E:\\Java\\a.txt");
FileReader fr2 = new FileReader("E:\\Java\\b.txt");
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
BufferedWriter bw3 = new BufferedWriter(new FileWriter("E:\\Java\\c.txt"));
String s, s1, s2;
s = br1.readLine();
s1 = br2.readLine();
while (s != null) {
bw3.write(s);
s = br1.readLine();
bw3.newLine();
}
while (s1 != null) {
bw3.write(s1);
s1 = br1.readLine();
bw3.newLine();
}
br1.close();
br2.close();
bw3.close();
System.out.println("文件合并成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:
两个文件合并到第三个文件
·先调试后运行