Home » Java PrintStream class

Java PrintStream class

by Online Tutorials Library

java.io.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.

Commonly used methods of PrintStream class

There are many methods in PrintStream class. Let’s see commonly used methods of PrintStream class:
  • public void print(boolean b): it prints the specified boolean value.
  • public void print(char c): it prints the specified char value.
  • public void print(char[] c): it prints the specified character array values.
  • public void print(int i): it prints the specified int value.
  • public void print(long l): it prints the specified long value.
  • public void print(float f): it prints the specified float value.
  • public void print(double d): it prints the specified double value.
  • public void print(String s): it prints the specified string value.
  • public void print(Object obj): it prints the specified object value.
  • public void println(boolean b): it prints the specified boolean value and terminates the line.
  • public void println(char c): it prints the specified char value and terminates the line.
  • public void println(char[] c): it prints the specified character array values and terminates the line.
  • public void println(int i): it prints the specified int value and terminates the line.
  • public void println(long l): it prints the specified long value and terminates the line.
  • public void println(float f): it prints the specified float value and terminates the line.
  • public void println(double d): it prints the specified double value and terminates the line.
  • public void println(String s): it prints the specified string value and terminates the line./li>
  • public void println(Object obj): it prints the specified object value and terminates the line.
  • public void println(): it terminates the line only.
  • public void printf(Object format, Object… args): it writes the formatted string to the current stream.
  • public void printf(Locale l, Object format, Object… args): it writes the formatted string to the current stream.
  • public void format(Object format, Object… args): it writes the formatted string to the current stream using specified format.
  • public void format(Locale l, Object format, Object… args): it writes the formatted string to the current stream using specified format.

Example of java.io.PrintStream class

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

Example of printf() method of java.io.PrintStream class:

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

You may also like