Java PrintWriter class is the implementation of Writer class. It is used to print the formatted representation of objects to the text-output stream. PrintWriter is a class used to write any form of data, e.g. int, float, double, String or Object in the form of text either on the console or in a file in Java.” For example, you may use the PrintWriter object to log data in a file or print it on the console.
Class declaration
Let’s see the declaration for Java.io.PrintWriter class:
public class PrintWriter extends Writer
Methods of PrintWriter class:
| Method | Description |
| void println(boolean x) | It is used to print the boolean value. |
| void println(char[] x) | It is used to print an array of characters. |
| void println(int x) | It is used to print an integer. |
| PrintWriter append(char c) | It is used to append the specified character to the writer. |
| PrintWriter append(CharSequence ch) | It is used to append the specified character sequence to the writer. |
| PrintWriter append(CharSequence ch, int start, int end) | It is used to append a subsequence of a specified character to the writer. |
| boolean checkError() | It is used to flush the stream and check its error state. |
| protected void setError() | It is used to indicate that an error occurs. |
| protected void clearError() | It is used to clear the error state of a stream. |
| PrintWriter format(String format, Object… args) | It is used to write a formatted string to the writer using specified arguments and format string. |
| void print(Object obj) | It is used to print an object. |
| void flush() | It is used to flush the stream. |
| void close() | It is used to close the stream. |
Java PrintWriter Example:
Let’s see the simple example of writing the data on a console and in a text file testout.txt using Java PrintWriter class.
import java.io.File;
import java.io.PrintWriter;
public class TechVidvan{
public static void main(String[] args) throws Exception {
PrintWriter writer = new PrintWriter(System.out);
writer.write("TechVidvan provides tutorials of all technology.");
writer.flush();
writer.close();
PrintWriter writer1 =null;
writer1 = new PrintWriter(new File("D:\\testout.txt"));
writer1.write("Like Java, Spring, Hibernate, Android, PHP etc.");
writer1.flush();
writer1.close();
}
}
Constructor and Description:
- PrintWriter(File file): Creates a new PrintWriter, without automatic line flushing, with the specified file.
- PrintWriter(File file, String csn): Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
- PrintWriter(OutputStream out): Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
- PrintWriter(OutputStream out, boolean autoFlush): Creates a new PrintWriter from an existing OutputStream.
- PrintWriter(String fileName): Creates a new PrintWriter, without automatic line flushing, with the specified file name.
- PrintWriter(String fileName, String csn): Creates a new PrintWriter without automatic line flushing and with the specified file name and charset.
- PrintWriter(Writer out): Creates a new PrintWriter without automatic line flushing.
- PrintWriter(Writer out, boolean autoFlush): Creates a new PrintWriter.
Methods of PrintWriter class:
1. PrintWriter append(char c)
Appends the specified character to this writer
Syntax:
public PrintWriter append(char c)
Parameters:
c – The 16-bit character to append
Returns:
This writer
2. PrintWriter append(CharSequence csq, int start, int end)
Appends the specified character sequence to this writer.
Syntax:
public PrintWriter append(CharSequence csq,
int start,int end)
Parameters:
csq – The character sequence from which a subsequence will be appended.
start – The index of the first character in the subsequence
end – The index of the character following the last character in the subsequence
Returns:
This writer
Throws:
IndexOutOfBoundsException
3. PrintWriter append(CharSequence csq)
Appends a subsequence of the specified character sequence to this writer.
Syntax:
public PrintWriter append(CharSequence csq)
Parameters:
csq – The character sequence to append.
Returns:
This writer
4. boolean checkError()
Flushes the stream and checks its error state.
Syntax:
public boolean checkError()
Returns: true if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked
5. protected void clearError()
Clears the internal error state of this stream.
Syntax:
protected void clearError()
6. void close()
Closes the stream and releases any system resources associated with it.
Syntax:
public void close()
Specified by: close in class Writer
7. void flush()
Flushes the stream.
Syntax:
public void flush()
Specified by: flush in interface Flushable
Specified by: flush in class Writer
8. PrintWriter format(Locale l, String format, Object… args)
Write a formatted string to this writer using the specified format string and arguments.
Syntax:
public PrintWriter format(Locale l,
String format,
Object... args)
Parameters:
l – The locale is to be applied during formatting. If l is null, then no localization is applied.
format – A format string as described in Format string syntax
args – Arguments referenced by the format specifiers in the format string. The number of arguments is variable and may be zero.
Returns:
This writer
Throws:
IllegalFormatException
NullPointerException
9. PrintWriter format(String format, Object… args)
Write a formatted string to this writer using the specified format string and arguments.
Syntax:
public PrintWriter format(String format,
Object... args)
Parameters:
format – A format string as described in Format string syntax
args – Arguments referenced by the format specifiers in the format string. The number of arguments is variable and may be zero.
Returns:
This writer
Throws:
IllegalFormatException
NullPointerException
10. void print(boolean b)
Prints a boolean value.
Syntax:
public void print(boolean b)
11. void print(char c)
Prints a character.
Syntax:
public void print(char c)
12. void print(char[] s)
Prints an array of characters.
Syntax:
public void print(char[] s)
13. void print(double d)
Prints a double-precision floating-point number.
Syntax:
public void print(double b)
14. void print(float f)
Prints a floating-point number.
Syntax:
public void print(float f)
15. void print(int i)
Prints an integer.
Syntax:
public void print(int i)
16. void print(long l)
Prints a long integer.
Syntax:
public void print(long l)
17. void print(Object obj)
Prints an object.
Syntax:
public void print(Object obj)
18. void print(String s)
Prints a string.
Syntax:
public void print(String s)
Conclusion
We can write formatted data for an underlying writer using the Java PrintWriter class java. A superclass of the PrintWriter is the Writer class. It is easier to customize the format as per the specified Locale (regional standards) while publishing global applications using the PrintWriter object.

