Java Formatter(File file) Constructor

Syntax

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

public Formatter(File file)   throws FileNotFoundException

Example

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


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
/*  w  w  w.ja  v a 2  s  .  c  o m*/
public class Main {

   public static void main(String[] args) throws FileNotFoundException {


      Formatter formatter = new Formatter(new File("generated/format.txt"));

      // format a new string
      String name = "from java2s.com";
      formatter.format("Hello %s !", name);

      // print the formatted string
      System.out.println(formatter);

      // flush the formatter. Here it does nothing.
      formatter.flush();
      System.out.println("Formatter Flushed.");
   }
}