• 字符流复制文件的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();
    
             }
    }
     
  • 相关阅读:
    TLPI读书笔记第32章:线程取消
    TLPI读书笔记第30章:线程同步
    Javascript事件:this.value()和this.select()
    Unknown tag (s:property)的原因
    maven web项目中运行stucts2报404的解决方案
    maven web项目的web.xml报错The markup in the document following the root element must be well-formed.
    JAVA实现 springMVC方式的微信接入、实现消息自动回复
    jquery easyui datagrid使用参考
    SQL两个字段排序
    windows下 zookeeper dubbo 安装+配置+demo 详细图文教程
  • 原文地址:https://www.cnblogs.com/csslcww/p/9179330.html
Copyright © 2020-2023  润新知