The PrintWriter
class of the java.io
package can be used to write output data in a commonly readable form (text).
It extends the abstract class Writer
.
Working of PrintWriter
Unlike other writers, PrintWriter
converts the primitive data(int
, float
, char
, etc.) into the text format. It then writes that formatted data to the writer.
Also, the PrintWriter
class does not throw any input/output exception. Instead, we need to use the checkError()
method to find any error in it.
Note: The PrintWriter
class also has a feature of auto flushing. This means it forces the writer to write all data to the destination if one of the println()
or printf()
methods is called.
Create a PrintWriter
In order to create a print writer, we must import the java.io.PrintWriter
package first. Once we import the package here is how we can create the print writer.
1. Using other writers
// Creates a FileWriter
FileWriter file = new FileWriter("output.txt");
// Creates a PrintWriter
PrintWriter output = new PrintWriter(file, autoFlush);
Here,
- we have created a print writer that will write data to the file represented by the
FileWriter
- autoFlush is an optional parameter that specifies whether to perform auto flushing or not
2. Using other output streams
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream("output.txt");
// Creates a PrintWriter
PrintWriter output = new PrintWriter(file, autoFlush);
Here,
- we have created a print writer that will write data to the file represented by the
FileOutputStream
- the autoFlush is an optional parameter that specifies whether to perform auto flushing or not
3. Using filename
// Creates a PrintWriter
PrintWriter output = new PrintWriter(String file, boolean autoFlush);
Here,
- we have created a print writer that will write data to the specified file
- the autoFlush is an optional boolean parameter that specifies whether to perform auto flushing or nor
Note: In all the above cases, the PrintWriter
writes data to the file using some default character encoding. However, we can specify the character encoding (UTF8 or UTF16) as well.
// Creates a PrintWriter using some character encoding
PrintWriter output = new PrintWriter(String file, boolean autoFlush, Charset cs);
Here, we have used the Charset class to specify the character encoding. To know more, visit Java Charset (official Java documentation).
Methods of PrintWriter
The PrintWriter
class provides various methods that allow us to print data to the output.
print() Method
print()
- prints the specified data to the writerprintln()
- prints the data to the writer along with a new line character at the end
For example,
import java.io.PrintWriter;
class Main {
public static void main(String[] args) {
String data = "This is a text inside the file.";
try {
PrintWriter output = new PrintWriter("output.txt");
output.print(data);
output.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have created a print writer named output. This print writer is linked with the file output.txt.
PrintWriter output = new PrintWriter("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 formatted string. It includes 2 parameters: formatted string and arguments. For example,
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 formatted string includes both text and data. And, the arguments replace the data inside the formatted string.
Hence the %d is replaced by 25.
Example: printf() Method using PrintWriter
import java.io.PrintWriter;
class Main {
public static void main(String[] args) {
try {
PrintWriter output = new PrintWriter("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 created a print writer named output. The print writer is linked with the file output.txt.
PrintWriter output = new PrintWriter("output.txt");
To print the formatted text to the file, we have used the printf()
method.
Here when we run the program, the output.txt file is filled with the following content.
I am 25 years old.
Other Methods Of PrintWriter
Method | Description |
---|---|
close() |
closes the print writer |
checkError() |
checks if there is an error in the writer and returns a boolean result |
append() |
appends the specified data to the writer |
To learn more, visit Java PrintWriter (official Java documentation).