new Formatter(new OutputStream('test.fmt')) : Formatter « Development « Java Tutorial






import java.io.FileOutputStream;
import java.util.Formatter;

public class Main {
  public static void main(String[] argv) throws Exception {
    Formatter fmtCon = new Formatter(System.out);
    Formatter fmtFile;
    fmtFile = new Formatter(new FileOutputStream("test.fmt"));
    fmtCon.format("a negative number: %(.2f\n\n", -123.34);
    fmtCon.format("%8s %8s\n", "Value", "Square");

    for (int i = 1; i < 20; i++)
      fmtCon.format("%8d %8d\n", i, i * i);

    // write to the file.
    fmtFile.format("This is a negative number: %(.2f\n\n", -123.34);

    fmtFile.format("%8s %8s\n", "Value", "Square");
    for (int i = 1; i < 20; i++)
      fmtFile.format("%8d %8d\n", i, i * i);

    fmtFile.close();

    if (fmtFile.ioException() != null) {
      System.out.println("File I/O Error Occurred");
    }
  }
}








6.4.Formatter
6.4.1.Write formatted output directly to the console and to a file.
6.4.2.Formatter.ioException()
6.4.3.new Formatter(new OutputStream('test.fmt'))
6.4.4.locale-specific formatting.