PrintStream

The PrintStream is designed to print data at DOS prompt with its print() and println() methods. This stream comes with the ability of automatic flushing of data. In contrary to the methods of all I/O streams, the methods of PrintStream class do not throw IOException. PrintStream methods are deprecated in favor of PrintWriter, but not System.out. There exists a similar class on character streams side, PrintWriter with println() method.

The println() and print() methods are overloaded many times in the PrintStream class to accept different data types and objects as parameters. println() method is overloaded 10 times and print() method is overloaded 9 times.

Writing at DOS prompt

In the code, both println() and print() methods are used to print at the DOS prompt.

import java.io.*;
public class PSDemo
{
  public static void main(String args[]) throws IOException
  {
    PrintStream pstream = new PrintStream(System.out);
    pstream.println(100);	
    pstream.println("World");
    pstream.print(false);  
    pstream.println(new Double(10.5));
        
    System.out.println("Java is simple but ocean");
    System.err.println("Practice it carefully");   
      		
    pstream.close();
  }
}

PrintStream

Observe, the false and 10.5 come in the same line as false is printed with print() method and not with println() method.

Leave a Comment

Your email address will not be published.