Java I/O How to - Create PrintWriter from File name








Question

We would like to know how to create PrintWriter from File name.

Answer

 // w w w .j  a va 2  s .  c om

import java.io.IOException;
import java.io.PrintWriter;

public class MainClass {
  public static void main(String[] args) {
    try {
      PrintWriter pw = new PrintWriter("c:\\temp\\printWriterOutput.txt");
      pw.println("PrintWriter is easy to use.");
      pw.println(1234);
      pw.close();
    } catch (IOException e) {
    }
  }
}

The code above generates the following result.