字符流复制文件的5种方式

package day21_io.test;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

 

/**

 *

 * 复制文本文件

 *

 * 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。 通过该原理,我们知道我们应该采用字符流更方便一些。

 * 而字符流有5种方式,所以做这个题目我们有5种方式。推荐掌握第5种。 数据源: c:\\a.txt -- FileReader --

 * BufferdReader 目的地: d:\\b.txt -- FileWriter -- BufferedWriter

 *

 */

public class test7 {

         public static void main(String[] args) throws IOException {

                   String srcString = "c:\\a.txt";

                   String distString = "d:\\b.txt";

                   // method1(srcString, distString);

                   // method2(srcString, distString);

                   // method3(srcString,distString);

                   // method4(srcString,distString);

                   method5(srcString, distString);

         }
 

 

         // 字符 缓存(高效)流,一次读写一个字符串

      

   private static void method5(String srcString, String distString) throws IOException {

                   BufferedReader br = new BufferedReader(new FileReader(srcString));

                   BufferedWriter bw = new BufferedWriter(new FileWriter(distString));

                   String line = null;

                   while ((line = br.readLine()) != null) {

                            bw.write(line);

                            bw.newLine();

                            bw.flush();

                   }

                   bw.close();

                   br.close();

         }
 

 

         // 缓存(高效)字符流,一次读写一个字符数组

        

 private static void method4(String srcString, String distString) throws IOException {

                   BufferedReader br = new BufferedReader(new FileReader(srcString));

                   BufferedWriter bw = new BufferedWriter(new FileWriter(distString));

                   char[] chs = new char[1024];

                   int len = 0;

                   while ((len = br.read(chs)) != -1) {

                            bw.write(chs, 0, len);

                   }

                   bw.close();

                   br.close();

         }

 
 

         // 缓存(高效)字符流,一次读写一个字符

       

  private static void method3(String srcString, String distString) throws IOException {

                   BufferedReader br = new BufferedReader(new FileReader(srcString));

                   BufferedWriter bw = new BufferedWriter(new FileWriter(distString));

 

                   int ch = 0;

                   while ((ch = br.read()) != -1) {

                            bw.write(ch);

                   }

                   bw.close();

                   br.close();

         }
 

 

         // 基本字符流,一次读写一个字符数组

         

private static void method2(String srcString, String distString) throws IOException {

                   FileReader fr = new FileReader(srcString);

                   FileWriter fw = new FileWriter(distString);

                   char[] chs = new char[1024];

                   int len = 0;

                   // 注意,这里的read有参数字符数组

                   while ((len = fr.read(chs)) != -1) {

                            fw.write(chs, 0, len);

                   }

                   fw.close();

                   fr.close();

         }
 

 

         // 基本字符流,一次读写一个字符

      

   private static void method1(String srcString, String distString) throws IOException {

                   FileReader fr = new FileReader(srcString);

                   FileWriter fw = new FileWriter(distString);

                   int ch = 0;

                   while ((ch = fr.read()) != -1) {

                            fw.write(ch);

                   }

                   fw.close();

                   fr.close();

         }
}
 

 

posted @ 2018-06-13 18:53  深沉有点事  阅读(570)  评论(0编辑  收藏  举报