Signup/Sign In
PUBLISHED ON: FEBRUARY 24, 2021

Java PrintStream Class

In this tutorial, we will learn about PrintStream class in Java. This class provides various methods to write data to another stream. This class converts primitive data into the text format and writes it to the output stream.

enlightenedUnlike other output streams, a PrintStream never throws an IOException; alternatively, exceptional situations set an internal flag that can be tested by the checkError method.

Syntax

This is the syntax declaration of PrintStream class.

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 the specified format.
void format(Locale l, Object format, Object... args) It writes the formatted string to the current stream using the specified format.

Example of PrintStream println() Method

In the program given below, we are implementing println() method from the PrintStream class and this method will print the data. we have two methods with little variation:

print() - This method prints the given data to the output stream.

println() - This method prints the data to the output stream and adds the new line to the end of the output.

In the below program we are not creating an instance of PrintStream class and still, we are able to directly call the print() method of PrintStream class. The reason is System is a final class where out is PrintStream type is declared.

package studytonight;
public class StudyTonight 
{
	public static void main(String args[])
	{
		System.out.println("Hello Studytonight"); 
	}
}


Hello Studytonight

Example of PrintStream print() Method

In the following example, we created an object of PrintStream class and this stream will be stored in the provided file. After calling print() method using the object of PrintStream class it will store the data to the given file. We can see the content of output.txt and verify it is working as expected.

package studytonight;
import java.io.PrintStream;
public class StudyTonight 
{
	public static void main(String args[])
	{
		String str = "Welcome to studytonight.com";
		try
		{
			PrintStream obj= new PrintStream("E:\\studytonight\\output.txt");
			obj.print(str);
			obj.close();
			System.out.print("Data is written to the file successfully...");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Data is written to the file successfully...

output.txt

Welcome to studytonight.com

Example of PrintStream

In the given example, we are implementing a print() method with two parameters first one is a formated string and the second parameter is a value. In this example %d will be replaced 12 and data will be written to file as I have 12 apples. This technique is known as formatting output.

package studytonight;
import java.io.PrintStream;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PrintStream output = new PrintStream("E:\\studytonight\\output.txt");
			int num = 12;
			output.printf("I have %d apples.", num);
			output.close();
			System.out.print("Data is written to the file successfully...");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Data is written to the file successfully...

output.txt

I have 12 apples.

Conclusion

In this tutorial, we learned about the PrintStream class in Java. This class provides various methods to write data to another stream. This class converts primitive data into the text format and writes it to the output stream.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.