02026_字符流练习

1、练习:复制文本文件

2、思路:

  (1)既然是文本涉及编码表。需要用字符流;

  (2)操作的是文件。涉及硬盘;

  (3)有指定码表吗?没有,默认就行。

 1 import java.io.FileReader;
 2 import java.io.FileWriter;
 3 import java.io.IOException;
 4 
 5 public class CopyTextFileTest {
 6     public static void main(String[] args) throws IOException {
 7         copyTextFile();
 8     }
 9 
10     public static void copyTextFile() throws IOException {
11         // 1,明确源和目的。
12         FileReader fr = new FileReader("d:\\Java\\cn.txt");
13         FileWriter fw = new FileWriter("d:\\Java\\copy.txt");
14         // 2,为了提高效率。自定义缓冲区数组。字符数组。
15         char[] buf = new char[1024];
16         int len = 0;
17         while ((len = fr.read(buf)) != -1) {
18             fw.write(buf, 0, len);
19         }
20         /*
21          * 2,循环读写操作。效率低。 int ch = 0; while((ch=fr.read())!=-1){ fw.write(ch); }
22          */
23         // 3,关闭资源。
24         fw.close();
25         fr.close();
26     }
27 }

 

posted @ 2017-12-24 08:09  gzdlh  阅读(207)  评论(0编辑  收藏  举报