Get IO exception information from Formatter in Java

Description

The following code shows how to get IO exception information from Formatter.

Example


/*  ww  w.  ja  v a2  s  .  c  om*/
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);

    fmtFile.close();

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

The code above generates the following result.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter