BufferedWriter与BufferedRead --------------------------Test2

 package com.test;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 将两个文档合并为一个文档,需要注意要读取的文件的编码格式与java文件的编码格式相同
* @author Administrator
*
*/
public class Demo3 { public static void main(String[] args) {
// TODO Auto-generated method stub
Readwrite111 readwrite111 = new Readwrite111();
readwrite111.copyDate(); } } class Readwrite111 {
public static void copyDate() {
BufferedReader bufferedReader = null;
BufferedReader bufferedReader2 = null;
BufferedWriter bufferedWriter = null;
try {
//将要进行合并的文档
File file = new File("D://userMsg//userinfo.txt");
File file2 = new File("D://userMsg//userinfo11.txt");
//保存读取到的文档内容
List<String> list = new ArrayList<String>();
String str = null;
//开始进行第一个文档的读取
FileReader fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
while((str = bufferedReader.readLine())!= null){
list.add(str); }
//开始进行第二个文档的读取
FileReader fileReader2 = new FileReader(file2);
bufferedReader2 = new BufferedReader(fileReader2);
while((str = bufferedReader2.readLine())!=null){
list.add(str); }
//将要进行合并的文档,并此将结果写入到此文档中
File file3 = new File("D://userMsg//userinfo22.txt");
FileWriter fileWriter = new FileWriter(file3);
bufferedWriter = new BufferedWriter(fileWriter);
for(String s : list){
bufferedWriter.write(s);
//写入数据是进行换行
bufferedWriter.write("\r\n");
System.out.println(s);
}
bufferedWriter.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally{
try {
if(bufferedReader!=null){
bufferedReader.close();
}
if(bufferedReader2!=null){
bufferedReader2.close();
}
if(bufferedWriter!=null){
bufferedWriter.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
上一篇:《机器学习实战》学习笔记第十四章 —— 利用SVD简化数据


下一篇:【Unity Shaders】学习笔记——SurfaceShader(四)用纹理改善漫反射