Java Format - Java Formatter(PrintStream ps) Constructor








Syntax

Formatter(PrintStream ps) constructor from Formatter has the following syntax.

public Formatter(PrintStream ps)

Example

In the following code shows how to use Formatter.Formatter(PrintStream ps) constructor.

//  w  w  w .  ja v a2s .  c o  m
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Formatter;
import java.util.Locale;

public class Main {

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


      Formatter formatter = new Formatter(new PrintStream(new FileOutputStream("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.");
   }
}