package Zuoye01; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class Copy { public static void main(String[] args) { System.out.println(System.getProperty("file.encoding")); //复制和粘贴 用BufferRead 和 BufferWrite //复制应该先读取 输入 文件 => 程序 Reader r = null; BufferedReader er = null; //再输出 应该从程序写 粘贴 程序 => 文件 Writer w = null; BufferedWriter ew = null; //找到要读取的文件地址 try { r = new FileReader("D:/MyDemo/Demo.txt"); er = new BufferedReader(r); //要写一下要复制到哪里的地址 try { w = new FileWriter("D:/MyDemo/t.txt"); ew = new BufferedWriter(w); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //读取 的时候可以 用Buffere的ReadLine方法 一行的读效率会比较高 String line = null; try { while((line = er.readLine())!=null) { //这里的line是字符串,所以可以直接这样写: //调用BufferedWrite的Write 把line字符串传进去 ew.write(line); ew.flush(); ew.newLine(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { ew.close(); w.close(); er.close(); r.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }