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