English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java PrintWriter and its print() and printf() methods through examples.
The PrintWriter class in the java.io package can be used to write output data in a readable form (text).
It inherits the abstract class Writer.
Unlike other writers, PrintWriter converts the original data (int, float, char, etc.) to text format. Then it writes the formatted data to the writer.
Additionally, the PrintWriter class does not throw any input/Output exception. Instead, we need to use the checkError() method to find any errors in it.
autoFlush is an optional boolean parameter that specifies whether to perform automatic flushing: The PrintWriter class also has an automatic flushing feature. This means that if one of the println() or printf() methods is called, it will force the writer to write all data to the target.
To create a print writer, we must import the java.io.PrintWriter package. After importing the package, we can create a print writer.
1.Use other writers
//Create FileWriter FileWriter file = new FileWriter("output.txt"); //.Using the filename PrintWriter output = new PrintWriter(file, autoFlush);
);
We created a print writer that writes data to a FileWriter
autoFlush is an optional parameter used to specify whether to perform automatic refresh
2.Use other output streams
// Creates a FileOutputStream FileOutputStream file = new FileOutputStream("output.txt"); // Creates a PrintWriter PrintWriter output = new PrintWriter(file, autoFlush);
);
We created a print writer that writes data to a file represented by FileWriter
autoFlush is an optional parameter used to specify whether to perform automatic flushing
3.使用文件名
//.Using the filename Create PrintWriter
);
PrintWriter output = new PrintWriter(String file, boolean autoFlush);
We have created a printer writer that writes data to the specified file
autoFlush is an optional boolean parameter that specifies whether to perform automatic flushingNoteor8In all the above cases, PrintWriter writes data to the file using certain default character encodings. However, we can also specify the character encoding (or16UTF
//) Create a PrintWriter using certain character encoding
PrintWriter output = new PrintWriter(String file, boolean autoFlush, Charset cs); Here, we usedCharacter set
PrintWriter methods
print() method - print();
Print the specified data to the writer - println();
import java.io.PrintWriter; class Main { public static void main(String[] args) { Print the data along with a newline character to the writer try { PrintWriter output = new PrintWriter("output.txt"); String data = "This is a text inside the file."; output.close(); } catch(Exception e) { e.getStackTrace(); } } }
output.print(data);output.txt.
PrintWriter output = new PrintWriter("output.txt");
In the above example, we created a printer writer named output. This printer writer is linked to the file
Here, when we run the program,output.txtThe file will be filled with the following content.
To print data to the file, we used the print() method.
printf() method2The printf() method can be used to print formatted strings. It contains
A parameter: the formatted string and the parameters. For example, 25printf("I %d years old",
);
Here,
I am %d years old is a formatted string
25 %d is the integer data in the formatted string
It is a parameter
Therefore, the formatted string includes text and data. Moreover, the parameters replace the data in the formatted string.%dReplace with25.
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 created a printer writer named output. The printer writer is linked to the file output.txt.
PrintWriter output = new PrintWriter("output.txt");
To print formatted text to the file, we use the printf() method.
Here, when we run the program,output.txtThe file will be filled with the following content.
I am 25 years old.
Method | Description |
---|---|
close() | Close PrintWriter |
checkError() | Check if there are any errors in the writer and return a boolean result |
append() | Append the specified data to the writer |