Java I/O How to - Write formatted output directly to the console and to a file








Question

We would like to know how to write formatted output directly to the console and to a file.

Answer

/*  w  w  w. j  av  a 2s  .c  o  m*/
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");
    }
  }
}

The code above generates the following result.