• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java PrintWriter Example

April 20, 2014 //  by Krishna Srinivasan//  Leave a Comment

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

Category: JavaTag: Java File IO

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.

Previous Post: « Java BufferedOutputStream Example
Next Post: Java PrintStream Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact