Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java PrintWriter class

In this tutorial, we will learn about the PrintWriter class in Java. This class is used to write the formatted representation of objects to the text-output stream. This class converts the primitive data into the text format, then it writes this converted data to the writer.

Syntax

This is the syntax of PrintWriter class. This class extends the Writer class.

public class PrintWriter extends Writer  

Methods of PrintWriter Class

All the supported methods of PrintWriter class are given in the table below.

Method Description
void println(boolean x) This method is used to print the boolean value.
void println(char[] x) This method is used to print an array of characters.
void println(int x) This method is used to print an integer.
PrintWriter append(char c) This method is used to append the specified character to the writer.
PrintWriter append(CharSequence ch) This method is used to append the specified character sequence to the writer.
PrintWriter append(CharSequence ch, int start, int end) This method is used to append a subsequence of a specified character to the writer.
boolean checkError() This method is used to flushes the stream and checks its error state.
protected void setError() This method is used to indicate that an error occurs.
protected void clearError() This method is used to clear the error state of a stream.
PrintWriter format(String format, Object... args) This method is used to write a formatted string to the writer using specified arguments and format string.
void print(Object obj) This method is used to print an object.
void flush() This method is used to flushes the stream.
void close() This method is used to close the stream.

Example of PrintWriter

In this example, we are writing data on the console. Firstly we created an object of PrintWriter class, by passing System.in as an argument to the c, then by using this object, we call write() method of this class and the data will be printed on the console.

package studytonight;
import java.io.PrintWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		PrintWriter writer = new PrintWriter(System.out);    
		writer.write("Welcome to Studytonight");        
		writer.flush();  
		writer.close();  
	}
}


Welcome to Studytonight

Example of PrintWriter

In this example, we are writing data to the file with the help of the PrintWriter class. This example is similar to the above example but while creating an object of PrintWriter class we passed a file in a constructor. After that write() method will write the data in the provided file.

package studytonight;
import java.io.File;
import java.io.PrintWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PrintWriter writer= new PrintWriter(new File("E:\\studytonight\\output.txt"));  
			writer.write("Hello World");                                                   
			writer.flush();  
			writer.close();  
			System.out.println("Data is written to the file successfully...");
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Data is written to the file successfully...

output.txt

Hello World

Conclusion

In this tutorial, we learned about, PrintWriter class in Java. This class is used to write the formatted representation of objects to the text-output stream. This class converts the primitive data into the text format, then it writes this converted data to the writer.