Java PrintWriter ClassLast 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 DeclarationThe following declaration shows how the PrintWriter class is defined: Constructors of PrintWriter ClassThe 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 ConstructorsThe following code shows how the constructors of the PrintWriter class are used. Methods of PrintWriter ClassThe PrintWriter class provides methods to print and format text data.
Examples of Java PrintWriter ClassThe following examples demonstrate how to use the Java PrintWriter class. Example 1: Writing Data to Console and File Using PrintWriterThe 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 PrintWriterThe following example demonstrates how to print formatted output using the printf() method of the PrintWriter class. Output Count: 10, Price: 99.99 Next TopicJava OutputStreamWriter Class |
We request you to subscribe our newsletter for upcoming updates.