Java IO Tutorial - Java PrintWriter(File file) Constructor








Syntax

PrintWriter(File file) constructor from PrintWriter has the following syntax.

public PrintWriter(File file)   throws FileNotFoundException

Example

In the following code shows how to use PrintWriter.PrintWriter(File file) constructor.

//from   ww  w.j  a v  a 2  s.com
import java.io.*;

public class Main {

   public static void main(String[] args) {
      try {
         PrintWriter pw = new PrintWriter(new File("c:/text.txt"));

         // append chars
         pw.append('H');
         pw.append('e');
         pw.append('l');
         pw.append('l');
         pw.append('o');

         // flush the writer
         pw.flush();
         pw.close();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}