java 使用BufferedReader、BufferedWriter实现文本文件的复制;

时间:2021-10-18 21:00:03

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedReaderWriterDemo 
{
public static void main(String[] args) 
{
File file = new File("d:\\a\\s.txt"); //源路径;
File file1 = new File("f:\\a\\s.txt");  //目标路径;
BufferedWriter bw = null;
BufferedReader br = null;
try 
{
br = new BufferedReader(new FileReader(file));
bw = new BufferedWriter(new FileWriter(file1));  
String str;
while((str = br.readLine()) != null)
{
bw.write(str); //将数组内容写入;
bw.newLine(); //换行;
bw.flush(); //刷新;
}
} catch (Exception e) 
{
e.printStackTrace();
}finally
{
if(br != null)
{
try 
{
br.close();
} catch (IOException e) 
{
e.printStackTrace();
}
}
else if(bw != null)
{
try 
{
bw.close();
} catch (IOException e) 
{
e.printStackTrace();
}
}
}
}
}