dsa

Java PrintStream Class

In this tutorial, we will learn about Java PrintStream and its methods with the help of examples.

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically. All characters printed by a PrintStream are converted into bytes using the platform’s default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.

The PrintStream class is a subclass of the Java OutputStream.

Declaration

public class PrintStream
  extends FilterOutputStream
    implements Appendable, Closeable

Constructors :

Commonly used methods of PrintStream class

Example of java.io.PrintStream class

Let's see the simple example of printing integer value by format specifier.

class PrintStreamTest{  
 public static void main(String args[]){  
   int a=10;  
   System.out.printf("%d",a);//Note, out is the object of PrintStream class  
      
 }  
}   
Output:10