import java.io.*;
class Test{
public static void main(String[] args){
BufferedReader br=null;
BufferedWriter bw =null;
try{
br=new BufferedReader(new FileReader(new File("file.txt")));
bw=new BufferedWriter(new FileWriter(new File("file1.txt")));
//方式一:使用char[]数组
char[] cbuf=new char[1024];
int len;
while((len=br.read(cbuf))!=-1){
bw.write(cbuf,0,len);
bw.flush();
}
//方式二:使用String
String data;
//每次读一行
while((data=br.readLine())!=null){
bw.write(data);
/*由于读取的时候是读到回车符为止,而回车符不会一起写入,
所以回车符不会读如,要手动加换行符
*/
bw.newLine();
bw.flush();
}
//方式一和方式二任取一种
}catch(IOException e){
e.printStackTrace();
}finally{
if(bw!=null){
try{
bw.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(br!=null){
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}