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 .
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.
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);
2. Using other output streams
// Creates a FileOutputStream FileOutputStream file = new FileOutputStream("output.txt"); // Creates a PrintWriter PrintWriter output = new PrintWriter(file, autoFlush);
3. Using filename
// Creates a PrintWriter PrintWriter output = new PrintWriter(String file, boolean autoFlush);
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).
The PrintWriter class provides various methods that allow us to print data to the output.
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.
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);
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.
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.
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 |