BufferedReader和BufferedWriter

package com.suning;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

public class BufferedStreamDemo {
  public static void main(String[] args) throws IOException {
   //copy1();
   //copy2();
    copy3();
    return;
  }

  // 拷贝文件方式一
  public static void copy1() throws IOException {
   BufferedReader br = new BufferedReader(new FileReader("a.txt"));
   BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
   int ch;
   while (-1 != (ch = br.read())) {
     bw.write((char)ch);
     System.out.print((char)ch);
   }
   bw.close();
   br.close();
   return;
  }

  // 拷贝文件方式二
  public static void copy2() throws IOException {
   BufferedReader br = new BufferedReader(new FileReader("a.txt"));
   BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
   char[] chs = new char[1024];
   int len;
   while (-1 != (len = br.read())) {
     bw.write(chs, 0, len);
     System.out.print(chs);
   }
   bw.close();
   br.close();
   return;
  }
  
  // 拷贝文件方式三 
  public static void copy3() throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("a.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
    String line;
    while (null != (line = br.readLine())) {
      bw.write(line);
      bw.newLine();
      bw.flush();
      System.out.println(line);
    }
    bw.close();
    br.close();
    return;
  }

}

你可能感兴趣的:(BufferedReader和BufferedWriter)