PrintStream vs PrintWriter

These two streams are carriers of data to a destination. Both look alike, but they differ in some finer concepts.

Basically, the OutputStream class (super class of PrintStream) is an abstract class, JDK 1.0, that includes methods to write a stream of bytes in a sequential order

The Writer class (super class of PrintWriter) is an abstract class, JDK 1.1, that includes methods to write a stream of characters in a sequential order

PrintStream (of PrintStream vs PrintWriter)

It is a byte stream introduced with JDK 1.0. The class is derived from FilterOutputStream. Being a byte stream it can be chained to another high-level byte stream for greater affect or combined functionality. The print stream object can be used to carry any data to any output stream.

Following is the class signature

public class PrintStream extends FilterOutputStream implements Appendable, Closeable

The following code writes data types to the file pqr.txt.

import java.io.*;
public class WriteData
{
  public static void main(String args[]) throws FileNotFoundException
  {
    FileOutputStream fostream = new FileOutputStream("pqr.txt");            
    BufferedOutputStream bostream = new BufferedOutputStream(fostream);
    PrintStream ps = new PrintStream(bostream); 
   
    int x = 10;
    double y = 10.5;
    boolean b = true;
    String str = "hello";
                                                // writes to file pqr.txt
    ps.println(x);
    ps.println(y);
    ps.println(b);
    ps.println(str);
    ps.close();
                                                // writes to DOS prompt
    PrintStream ps1 = new PrintStream(System.out);
    ps1.println(x);   ps1.println(y);    ps1.println(b);     ps1.println(str);
  }
}


PrintStream vs PrintWriter
Output screenshot of PrintStream vs PrintWriter

The above code prints at DOS prompt.

Generally all I/O streams throw IOException, but PrintStream does not. This facility is provided as PrintStream is used to write data to standard output stream (System.out) very often in the program; to avoid the mess of exception handling, it is just a convenience.

PrintStreamm object can be created to flush data automatically whenever println and print etc. methods are called.

  FileOutputStream fos = new FileOutputStream("pqr.txt");         
  BufferedOutputStream bos = new BufferedOutputStream(fos);
  PrintStream ps = new PrintStream(bos, true);                                         

The extra parameter flag true indicates automatic flush.

All the characters printed using PrintStream are converted internally into bytes as per the platform default character encoding.

PrintWriter (of PrintStream vs PrintWriter)

PrintStream is used to write data in bytes and PrintWriter is used where it is required to write data in characters. PrintStream is a byte stream (subclass of OutputStream) and PrintWriter is a character stream (subclass of Writer).

Following is the class signature

public class PrintWriter extends Writer

This class includes all the print methods of PrintStream but does not include the methods of drawing raw bytes. Most of the methods of PrintStream and PrintWriter do not throw exceptions but some constructors (not all) throw FileNotFoundException. The programmer, if required, can check any exceptions state with checkError() method.

PrintStream is used for binary output (doing extra conversion) where as PrintWriter is for text (character set) output.

The PrintStream uses platform dependent character encoding which increases platform dependency issues that causes sometimes problems when moving from one platform to another. With PrintWriter, typically we can specify, the encoding and thus can avoid platform dependencies. Internally, PrintStream converts bytes to characters as per encoding rules of the OS. But PrintWriter does the job straightaway (no char-byte conversion).

PrintStream stream = new PrintStream(output);

The above statement uses default encoding.

PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"));

With PrintWriter constructor, encoding UTF-8 is used; not platform dependent. This is the advantage with PrintWriter where the programmer can control the encoding style.

From JDK 1.4, Java API allows to specify character encoding with PrintStream also. Thus, the difference became very narrow but for auto flushing. With PrintWriter also, when auto flush is enabled, auto flushing is done whenever print methods are called.

Like print streams, many groups exist in Java. A few are given for your reference.

1. Wrapper classes
2. Listeners
3. Adapters
4. Layout managers
5. Byte streams
6. Character streams
7. Thread groups
8. Filter streams
9. Wrapper streams
10. Components
11. Containers

Leave a Comment

Your email address will not be published.