JavaPrintStream

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java PrintStream Class

The PrintStream class provides methods to write data to another stream. The PrintStream class automatically flushes the data so there is no need to call flush() method. Moreover, its methods don't throw IOException.

Class declaration

Let's see the declaration for Java.io.PrintStream class:

  1. public class PrintStream extends FilterOutputStream implements Closeable. Appendable

Methods of PrintStream class

Method

Description

void print(boolean b)

It prints the specified boolean value.

void print(char c)

It prints the specified char value.

void print(char[] c)

It prints the specified character array values.

void print(int i)

It prints the specified int value.

void print(long l)

It prints the specified long value.

void print(float f)

It prints the specified float value.

void print(double d)

It prints the specified double value.

void print(String s)

It prints the specified string value.

void print(Object obj)

It prints the specified object value.

void println(boolean b)

It prints the specified boolean value and terminates the line.

void println(char c)

It prints the specified char value and terminates the line.

void println(char[] c)

It prints the specified character array values and terminates the line.

void println(int i)

It prints the specified int value and terminates the line.

void println(long l)

It prints the specified long value and terminates the line.

void println(float f)

It prints the specified float value and terminates the line.

void println(double d)

It prints the specified double value and terminates the line.

void println(String s)

It prints the specified string value and terminates the line.

void println(Object obj)

It prints the specified object value and terminates the line.

void println()

It terminates the line only.

void printf(Object format, Object... args)

It writes the formatted string to the current stream.

void printf(Locale l, Object format, Object... args)

It writes the formatted string to the current stream.

void format(Object format, Object... args)

It writes the formatted string to the current stream using specified format.

void format(Locale l, Object format, Object... args)

It writes the formatted string to the current stream using specified format.

 

Example of java PrintStream class

In this example, we are simply printing integer and string value.

  1. package com.javatpoint;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.PrintStream;  
  5. public class PrintStreamTest{    
  6.  public static void main(String args[])throws Exception{    
  7.    FileOutputStream fout=new FileOutputStream("D:\testout.txt ");    
  8.    PrintStream pout=new PrintStream(fout);    
  9.    pout.println(2016);    
  10.    pout.println("Hello Java");    
  11.    pout.println("Welcome to Java");    
  12.    pout.close();    
  13.    fout.close();    
  14.   System.out.println("Success?");    
  15.  }    
  16. }    
  17.    

Output

Success...

The content of a text file testout.txt is set with the below data

2016
Hello Java
Welcome to Java

Example of printf() method using java PrintStream class:

Let's see the simple example of printing integer value by format specifier using printf() method of java.io.PrintStream class.

  1. class PrintStreamTest{    
  2.  public static void main(String args[]){    
  3.    int a=19;    
  4.    System.out.printf("%d",a); //Note: out is the object of printstream       
  5.  }    
  6. }   

Output

19