• 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 PrintStream Example

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

In my previous example I have explained about the PrintWriter for printing the formatted output to the console and file. This example demonstrates how to use the PrintStream. Note that PrintWriter and PrintStream defines the similar methods and work very much similar, except that PrintWriter writes the sequence of characters and PrintStream writes the stream of bytes. This is the main difference between these two classes.

Since Java 1.4 it is possible to specify the character encoding for a PrintStream. The differences between PrintStream and PrintWriter are only about auto flushing behavior and that a PrintStream cannot wrap a Writer. This class defines the following constructors:

  • PrintStream(File file) – Creates a new print stream, without automatic line flushing, with the specified file.
  • PrintStream(File file, String csn) – Creates a new print stream, without automatic line flushing, with the specified file and charset.
  • PrintStream(OutputStream out) – Creates a new print stream.
  • PrintStream(OutputStream out, boolean autoFlush) – Creates a new print stream.
  • PrintStream(OutputStream out, boolean autoFlush, String encoding) – Creates a new print stream.
  • PrintStream(String fileName) – Creates a new print stream, without automatic line flushing, with the specified file name.
  • PrintStream(String fileName, String csn) – Creates a new print stream, without automatic line flushing, with the specified file name and charset.

PrintStream Example

package javabeat.net.io;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;

/**
 * Java PrintStream Example
 *
 * @author Krishna
 *
 */
public class PrintStreamExample {

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

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

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

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

		// write in a file (deletes the lines if exist)
		PrintStream printStreamFile = null;
		Date date = new Date();
		try {
			printStreamFile = new PrintStream(fileName);
			intVar++;
			printStreamFile.println(date);
			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 (printStreamFile != null) {
				printStreamFile.close();
			}
		}

		PrintStream printStreamFile1 = null;
		Object newLine = System.getProperty("line.separator");
		try {
			FileOutputStream fileWriter = new FileOutputStream(fileName, true);
			printStreamFile1 = new PrintStream(fileWriter);
			printStreamFile1.println(newLine);
			printStreamFile1.println("Add a line");
			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 (printStreamFile1 != null) {
				printStreamFile1.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:55:05 IST 2014

Add a line

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 PrintWriter Example
Next Post: Append To File using FileOutputStream »

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