The PrintStream class offers functionalities for writing data to a different stream. Data is automatically flushed by the PrintStream class, eliminating the necessity to manually invoke the flush method. Additionally, the methods within the PrintStream class do not raise IOExceptions.
Class declaration
Let's examine the declaration of the Java.io.PrintStream class:
Example
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 characterarrayvalues. |
| 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 specifiedstringvalue. |
| 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 instance, we are merely displaying both integer and string values.
Example
package com.example;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintStreamTest{
public static void main(String args[])throws Exception{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt ");
PrintStream pout=new PrintStream(fout);
pout.println(2016);
pout.println("Hello Java");
pout.println("Welcome to Java");
pout.close();
fout.close();
System.out.println("Success?");
}
}
Output
Output
Success...
The data within a text file named testout.txt is as follows:
Example
2016
Hello Java
Welcome to Java
Example of printf method using java PrintStream class:
Let's consider a basic example that demonstrates how to print an integer value using a format specifier with the printf function from the java.io.PrintStream class.
Example
class PrintStreamTest{
public static void main(String args[]){
int a=19;
System.out.printf("%d",a); //Note: out is the object of printstream
}
}
Output