FileReader和FileWriter

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;
  }
}

推荐阅读更多精彩内容