Java之将两个文件中的内容合并到一个文件中

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 comp {
	public static void main(String[] args) {
		try {
			FileReader fr1=new FileReader("E:\\java\\newFile.txt");//读取newFile.txt的内容
			FileReader fr2=new FileReader("E:\\java\\newFile1.txt");//读取newFile1.txt的内容
			BufferedReader br1=new BufferedReader(fr1);
			BufferedReader br2=new BufferedReader(fr2);
			BufferedWriter bw3=new BufferedWriter(new FileWriter("E:\\java\\targetFile000.txt"));
			String s,s1,s2;
			s=br1.readLine();
			s1=br2.readLine();
			while(s!=null)
			{
				bw3.write(s);//写入targetFile000.txt文件
				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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	

	}

}

你可能感兴趣的:(java)