使用FileWriter;FileReader;对文件进行操作
1. package com.File_writer; 2. 3. import java.io.FileNotFoundException; 4. import java.io.FileReader; 5. import java.io.FileWriter; 6. import java.io.IOException; 7. 8. public class File_readerCopy { 9. public static void main(String[] args) throws IOException { 10. // 创建读与写对象 11. FileReader in=null; 12. FileWriter out =null; 13. try { 14. in =new FileReader("E:\\File_readerCopy.java"); 15. out=new FileWriter("D:\\测试.java"); 16. char[] chars = new char[1024 * 512]; 17. int readCount=0; 18. while ((readCount=in.read(chars))!=-1){ 19. out.write(chars,0,readCount); 20. } 21. } catch (FileNotFoundException e) { 22. throw new RuntimeException(e); 23. } catch (IOException e) { 24. throw new RuntimeException(e); 25. }finally { 26. if (out != null) { 27. out.close(); 28. } 29. if (in != null) { 30. in.close(); 31. } 32. } 33. } 34. }
通过创建读写对象,分别传入输入输出文件的路径,完成对数据的复制操作,通过控制字节的大小,完成对整个文件的拷贝。