1 package test_demo.fileoper; 2 3 import java.io.*; 4 5 /* 6 * 字符输入输出流操作,复制文件 7 * 使用缓冲流扩展,逐行复制 8 * */ 9 public class FileReadWriteOper { 10 public static void main(String args[]) { 11 //字符流输入流 12 FileReader fr = null; 13 //字符流输出流 14 FileWriter fw = null; 15 //缓冲输入流 16 BufferedReader br = null; 17 //缓冲输出流 18 BufferedWriter bw = null; 19 20 try { 21 fr = new FileReader(new File("C:\testdata\filedir\a.txt")); 22 br = new BufferedReader(fr); //扩容,类似加水管 23 fw = new FileWriter("C:\testdata\filedir\c.txt"); 24 bw = new BufferedWriter(fw); 25 //逐行复制 26 String line = br.readLine(); 27 while (line != null) { 28 bw.write(line); 29 bw.newLine(); //换行输出 30 line = br.readLine(); 31 } 32 System.out.println("文件复制成功!"); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } finally { 36 try { 37 //关闭流,顺序与打开相反 38 bw.close(); 39 br.close(); 40 fw.close(); 41 fr.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 } 47 }