Java text file copy line by line

Description

Java text file copy line by line

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader inputStream = null;
    PrintWriter outputStream = null;

    try {//  w  ww .  ja v  a2 s  .c  o  m
      inputStream = new BufferedReader(new FileReader("Main.java"));
      outputStream = new PrintWriter(new FileWriter("MainTest.txt"));

      String l;
      while ((l = inputStream.readLine()) != null) {
        outputStream.println(l);
      }
      System.out.println("done");
    } finally {
      if (inputStream != null) {
        inputStream.close();
      }
      if (outputStream != null) {
        outputStream.close();
      }
    }
  }
}



PreviousNext

Related