Java PrintWriter Example

Java provides PrintWriter for printing the well formatted output to the desired location.  This class takes the OutputStream or Writer as the input and prints the output with the human readable format. PrintWriter defines the following constructors:

  • 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, 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.

PrintWriter Example

Lets look at the example for PrintWriter class.
PrintWriterExample.java

package javabeat.net.io;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Java PrintWriter Example
 *
 * @author Krishna
 *
 */
public class PrintWriterExample {

	public static void main(String args[]) throws IOException {
		String fileName = "TextFile.txt";

		// Creating standard output as OutputStreamWriter
		PrintWriter printWriter = new PrintWriter(System.out, true);
		printWriter.println("This is example for PrintWriter!!");
		int intVar = 15;
		double dbVar = 20.0;
		printWriter.printf("i = %d and k = %f", intVar, dbVar);

		// flush the instance pw
		printWriter.flush();

		System.out.println("\n---------------------------------------");

		// write in a file (deletes the lines if exist)
		PrintWriter printWriterFile = null;
		Date date = new Date();
		try {
			printWriterFile = new PrintWriter(fileName);
			intVar++;
			printWriterFile.println(date);
			printWriterFile.write("Write something in a line. i = " + intVar);
			System.out.println("Write to the file successfully");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} finally {
			// closing the output stream (best practice)
			if (printWriterFile != null) {
				printWriterFile.close();
			}
		}

		PrintWriter printWriterFile1 = null;
		Object newLine = System.getProperty("line.separator");
		try {
			FileWriter fileWriter = new FileWriter(fileName, true);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
			printWriterFile1 = new PrintWriter(bufferedWriter);
			printWriterFile1.println(newLine);
			printWriterFile1.println("Add a line");
			printWriterFile1.write("!!!PrintWriter Example!!!", 4, 8);
			System.out.println("Add new lines to the file successfully");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// closing the output stream (best practice)
			if (printWriterFile1 != null) {
				printWriterFile1.close();
			}
		}
	}
}

Console Output…

This is example for PrintWriter!!
i = 15 and k = 20.000000
---------------------------------------
Write to the file successfully
Add new lines to the file successfully

File Output…

Sun Apr 20 11:39:07 IST 2014
Write something in a line. i = 16

Add a line
rintWrit

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*