In this tutorial, we will learn about the Java PrintStream class and its print() and printf() strategies with the help of examples.
The PrintStream class of the java.io package can be used to write output data in regularly readable structure (text) rather than bytes.
It extends the abstract class OutputStream.
In this article, you will learn-
Working of PrintStream
Dissimilar to other output streams, the PrintStream changes over the primitive data (integer, character) into the content configuration rather than bytes. It at that point composes that designed data to the output stream.
And furthermore, the PrintStream class doesn’t toss any input/output exemption. All things considered, we need to use the checkError() technique to discover any mistake in it.
Note: The PrintStream class likewise has a component of auto flushing. This implies it forces the output stream to write all the data to the objective under one of the accompanying conditions:
- if newline character \n is written in the print stream
- if the println() method is invoked
- if an array of bytes is written in the print stream
Make a PrintStream
To make a PrintStream, we should import the java.io.PrintStream package first. When we import the package here is the means by which we can make the print stream.
- Using other output streams
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream(String file);
// Creates a PrintStream
PrintStream output = new PrintStream(file, autoFlush);
Here,
- we have made a print stream that will write arranged data to the record spoke to by FileOutputStream
- the autoFlush is a discretionary boolean parameter that determines if to perform auto flushing
2. Using filename
// Creates a PrintStream
PrintStream output = new PrintStream(String file, boolean autoFlush);
Here,
- we have created a print stream that will write formatted data to the specified file
- autoFlush is an optional boolean parameter that specifies whether to perform autoflush or not
Note: In both the case, the PrintStream write data to the file using some default character encoding. However, we can specify the character encoding (UTF8 or UTF16) as well.
// Creates a PrintStream using some character encoding
PrintStream output = new PrintStream(String file, boolean autoFlush, Charset cs);
Here, we have used the Charset class to specify the character encoding. To learn more, visit Java Charset (official Java documentation).
Methods of PrintStream
The PrintStream class provides various methods that allow us to print data to the output.
print() Method
- print() – prints the specified data to the output stream
- println() – prints the data to the output stream along with a new line character at the end
Example: print() method with System class
class Main {
public static void main(String[] args) {
String data = "Hello World.";
System.out.print(data);
}
}
Output
Hello World.
In the above example, we have not made a print stream. In any case, we can use the print() technique for the PrintStream class.
You may be thinking about how is this conceivable. Indeed, let me clarify what’s going on here.
Notice the line,
System.out.print(data);
Here,
- The system is a final class that is responsible to perform standard input/output operation
- out is a class variable of PrintStream type declared in System class
Now since out is of PrintStream type, we can use it to call all the methods of PrintStream class.
Example: print() method with PrintStream class
import java.io.PrintStream;
class Main {
public static void main(String[] args) {
String data = "This is a text inside the file.";
try {
PrintStream output = new PrintStream("output.txt");
output.print(data);
output.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have created a print stream named output. The print stream is linked with the output.txt file.
PrintStream output = new PrintStream("output.txt");
To print data to the file, we have used the print() method.
Here, when we run the program, the output.txt file is filled with the following content.
This is a text inside the file.
printf() Method
The printf() method can be used to print the organized string. It incorporates 2 parameters: designed string and contentions. For instance,
printf("I am %d years old", 25);
Here,
- I am %d years old is a formatted string
- %d is integer data in the formatted string
- 25 is an argument
The designed string incorporates both content and data. Also, the contentions supplant the data inside the arranged string.
Thus the %d is supplanted by 25.
Example: printf() method using PrintStream
import java.io.PrintStream;
class Main {
public static void main(String[] args) {
try {
PrintStream output = new PrintStream("output.txt");
int age = 25;
output.printf("I am %d years old.", age);
output.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have made a print stream named output. The print stream is connected with the record output.txt.
PrintStream output = new PrintStream("output.txt");
To print the arranged content to the file, we have used the printf() strategy.
Here, when we run the program, the output.txt file is loaded up with the accompanying substance.
I am 25 years old.
Other Methods Of PrintStream
Methods | Descriptions |
close() | closes the print stream |
checkError() | checks if there is an error in the stream and returns a boolean result |
append() | appends the specified data to the stream |
To learn more, visit Java PrintStream (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.
i love this optimum article