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.
Declaration
public class PrintStream
extends FilterOutputStream
implements Appendable, Closeable
Constructors :
- PrintStream(File file): Creates a new print stream, without automatic line flushing, with the specified file.
- PrintStream(File file, String csn) : Creates a new print stream, without automatic line flushing, with the specified file and charset.
- PrintStream(OutputStream out) : Creates a new print stream.
- PrintStream(OutputStream out, boolean autoFlush) : Creates a new print stream.
- PrintStream(OutputStream out, boolean autoFlush, String encoding) : Creates a new print stream.
- PrintStream(String fileName) : Creates a new print stream, without automatic line flushing, with the specified file name.
- PrintStream(String fileName, String csn) :Creates a new print stream, without automatic line flushing, with the specified file name and charset.
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
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