美文网首页
FileReader和FileWriter

FileReader和FileWriter

作者: March_Cullen | 来源:发表于2018-11-06 21:16 被阅读0次
    package com.suning;
    
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    
    public class FileWriteDemo {
      public static void main(String[] args) throws IOException {
          //copy1();
          copy2();
          return;
        }
    
      // 两种方式读写文件
      // 方式一
      public static void copy1() throws IOException {
        FileReader fr = new FileReader("FileWriterDemo.java");
        FileWriter fw = new FileWriter("c.txt");
        int ch;
        while (-1 != (ch = fr.read())) {
          fw.write((char)ch);
          System.out.println((char)ch);
        }
        fw.close();
        fr.close();
        return;
      }
    
      // 方式二 效率更高
      public static void copy2() throws IOException {
        FileReader fr = new FileReader("FileWriterDemo.java");
        FileWriter fw = new FileWriter("c.txt");
        char[] chs = new char[1024];
        int len;
        while (-1 != (len = fr.read(chs))) {
          fw.write(chs, 0, len);
          System.out.println(chs); 
        }
        fw.close();
        fr.close();
        return;
      }
    }
    

    相关文章

      网友评论

          本文标题:FileReader和FileWriter

          本文链接:https://www.haomeiwen.com/subject/hajpxqtx.html