In this tutorial we will learn about the the PrintWriter class in Java.
java.io.PrintWriter class is used for format printing of objects. To print these objects to the text output stream all the print method of PrintStream class is implemented by this class. This class extends Writer class and writes the character data. However, to write on the console in Java System.out is provided, except that the PrintWriter is also provided to write on console. PrintWriter is a character based class which makes the Java program easy for Internationalization. Objects of this class can be created by using various of its Constructors.
Constructor Details
| PrintWriter(File file) | This constructor is used to create a new PrintWriter object with the given file
name. Using this constructor output can be written to the file. This method
doesn't flushes line automatically. Syntax : public PrintWriter(File file) throws FileNotFoundException |
| PrintWriter(File file, String csn) | This constructor is used to create a new PrintWriter object with the given file
name and charset. Using this constructor output can be written to the file with
the specified charset. This method doesn't flushes line automatically. Syntax : public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException |
| PrintWriter(OutputStream out) | This constructor is used to create a new PrintWriter object with the OutputStream
already existed. This constructor doesn't flushes line automatically. Syntax : public PrintWriter(OutputStream out) |
| PrintWriter(OutputStream out, boolean autoFlush) | This method is used to create a new PrintWriter object with the OutputStream
already existed and a boolean value for auto flushing. Automatic flushing of line
based on the boolean value. Syntax : public PrintWriter(OutputStream out, boolean autoFlush) |
| PrintWriter(String fileName) | This constructor creates a new PrintWriter object with the given file name.
Using this constructor output can be written to the file. This method doesn't
flushes line automatically. Syntax : public PrintWriter(String fileName) throws FileNotFoundException |
| PrintWriter(String fileName, String csn) | This constructor creates a new PrintWriter object with the given file name
and specified charset. This method doesn't flushes the line automatically. Syntax : public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException |
| PrintWriter(Writer out) | This constructor is used to create a new PrintWriter object with an instance
of Writer (character-output stream). This method doesn't flushes the line automatically. Syntax : public PrintWriter(Writer out) |
| PrintWriter(Writer out, boolean autoFlush) | This constructor creates a new PrintWriter object with an instance of Writer
(character-output stream)
and a boolean value for auto flushing. Automatic flushing of line based on the
boolean value. Syntax : public PrintWriter(Writer out, boolean autoFlush) |
Methods Detail
Example
Here I am giving a simple example which will demonstrate you about how to use the PrintWriter in the Java applications. In this example I have created a PrintWriter object using the constructor PrintWriter(File file) and various of methods to print/write the characters to the file.
Source Code
JavaPrintWriterExample.java
import java.io.PrintWriter;
import java.io.File;
import java.util.Locale;
public class JavaPrintWriterExample {
public static void main(String[] args) {
String s = "PrintWriter";
int i = 50;
try {
File file = new File("file.txt");
// create a new PrintWriter with the specified file
PrintWriter pw = new PrintWriter(file);
// format text with specified locale.
// %s indicates a string will be placed there, which is s
pw.format(Locale.US, "This is a %s example", s);
// usage line.separator
pw.println();
// write integer
pw.println(i);
// write character
pw.write(i);
// usage line.separator
pw.println();
// format text with specified locale
// %d indicates a integer will be placed there, which is 100
pw.format("This is a %s example with %d", s, i);
System.out.println();
System.out.println("Data written to the file successfully");
// flush the writer
pw.flush();
PrintWriter pw1 = new PrintWriter(System.out);
pw1.println();
pw1.println("Data on console : ");
pw1.format(Locale.US, "This is a %s example", s);
pw1.println();
pw1.println(i);
pw1.write(i);
pw1.println();
pw1.format("This is a %s example with %d", s, i);
pw1.println();
pw1.flush();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
Output
When you will execute the above example you will get the output as follows :

Source code of this example can also be downloaded from the link given below.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java IO PrintWriter
Post your Comment