Java IO Tutorial - Java PrintStream

The PrintStream class is a concrete decorator for the output stream.

PrintStream can print any data type values, primitive or object, in a suitable format for printing.

PrintStream can write data to the output stream do not throw an IOException.

If a method throws an IOException, PrintStream sets an internal flag, rather than throwing the exception to the caller. The flag can be checked using its checkError() method, which returns true if an IOException occurs during the method execution.

PrintStream has an auto-flush capability. We can specify in its constructor that it should flush the contents written to it automatically.

If we set the auto-flush flag to true, PrintStream will flush its contents when a byte array is written, one of its overloaded println() methods is used to write data, a newline character is written, or a byte ('\n') is written.

/* w w w. j av  a  2s.c om*/
public class Main {
  public static void main(String[] args) {
    String destFile = "luci3.txt";

    try (PrintStream ps = new PrintStream(destFile)) {

      // flush the print stream

      System.out.println("Text has  been  written to "
          + (new File(destFile).getAbsolutePath()));
    } catch (FileNotFoundException e1) {

The code above generates the following result.