Java PrintWriter Class

Last Updated : 28 Jan 2026

The PrintWriter class in Java is used to write formatted text representations of objects to a character output stream.

In this chapter, we will learn what the PrintWriter class is, why it is used, its declaration, constructors, important methods, and how to write formatted data to the console and files using examples.

What is PrintWriter Class in Java?

The PrintWriter class is a character-based output stream that is used to print formatted representations of objects such as integers, doubles, strings, and other data types.

It simplifies writing text data by automatically handling line breaks and converting primitive data types into their textual form. The PrintWriter class also supports printf-style formatting and is often used with classes like FileWriter, BufferedWriter, or OutputStreamWriter to write data efficiently to files or other output destinations.

PrintWriter Class Declaration

The following declaration shows how the PrintWriter class is defined:

Constructors of PrintWriter Class

The PrintWriter class provides multiple constructors to create a writer connected to different output destinations.

1. PrintWriter(Writer out)

This constructor creates a new PrintWriter that writes to the specified Writer.

Syntax:

Here is the syntax:

2. PrintWriter(OutputStream out)

This constructor creates a new PrintWriter that writes to the specified output stream.

Syntax:

Here is the syntax:

3. PrintWriter(File file)

This constructor creates a new PrintWriter that writes to the specified file.

Syntax:

Here is the syntax:

4. PrintWriter(String fileName)

This constructor creates a new PrintWriter that writes to the file specified by its name.

Syntax:

Here is the syntax:

Example of PrintWriter Class Constructors

The following code shows how the constructors of the PrintWriter class are used.

Methods of PrintWriter Class

The PrintWriter class provides methods to print and format text data.

MethodDescription
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 specified character to the writer.
boolean checkError()It is used to flushes 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 flushes the stream.
void close()It is used to close the stream.
void println(double x)Prints a double value.
void println(float x)Prints a float value.
void println(long x)Prints a long value.
void println(String x)Prints a string.
void write(char[] buf)Writes an array of characters.
void write(char[] buf, int off, int len)Writes a portion of an array of characters.
void write(int c)Writes a single character.
void write(String s)Writes a string.
void write(String s, int off, int len)Writes a portion of a string.
PrintWriter format(Locale l, String format, Object... args)Writes a formatted string to the writer using the specified locale, format string, and arguments.
PrintWriter printf(String format, Object... args)Writes a formatted string to the writer using the specified format string and arguments.

Examples of Java PrintWriter Class

The following examples demonstrate how to use the Java PrintWriter class.

Example 1: Writing Data to Console and File Using PrintWriter

The following example demonstrates how to write text to the console and a file using the PrintWriter class.

Output

TpointTech provides tutorials of all technology.

File Content (testout.txt):

Like Java, Spring, Hibernate, Android, PHP etc.

Explanation

The PrintWriter class in Java is used by this code to write data to a file and the console. Text can be printed to the console by first constructing a PrintWriter object called writer, which is connected to the standard output stream.

To guarantee quick output, the println method publishes the given string to the console and then flushes it. Any system resources utilised by the PrintWriter are released using the shut function. Then, a new File object called "testout.txt" is attached to another PrintWriter, writer1, so that data can be written to that file in the current directory. Println, flush, and close methods are used to write the string to the file, guarantee that data is written instantly, and release resources in accordance with console output.

Example 2: Using printf() Method with PrintWriter

The following example demonstrates how to print formatted output using the printf() method of the PrintWriter class.

Output

Count: 10, Price: 99.99