Java.io - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io PrintStream

Java.io PrintStream

shape Introduction

The Java.io PrintStream is the class used to write the data which is formatted, to underlying output stream. The data by Java.io PrintStream is formatted in text including all primitive data types like int, byte, float, double. Any bytes of data formatted into text in case of print stream and then it written to output stream. There are two significant methods involved in print stream class, they are format() printf().

shape Conceptual figure

Class Declaration

shape Declaration

Java.io PrintStream class is declared below: Public class PrintStream extends FilterOutputStream implements Appendable, Closeable 

shape Fields

Java.io PrintStream class fields are:
  • Protected OutputStream out : The field represents the current stream have to filter.

Class Constructors

shape Table

Constructors Description
PrintStream(File file) The function of this constructor is to create a print stream with represented file and without flushing automatically.
PrintStream(OutputStream out) The function of this constructor is to create a print stream .
PrintStream(String fileName) The function of this constructor is to create a print stream with represented file name and without flushing automatically.
PrintStream(OutputStream out, Boolean autoFlush) The function of this constructor is to create a print stream with specified boolean auto flush.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
void flush () The function of this method is to flush the specified output stream.
void write (int b) The function of this method is to write the represented byte into output stream.
void write (byte[] buf, int off, int n) By using this method it writes the data up to 'n' bytes from the output stream to an array of bytes.
void println () The function of this method is to terminate the present line.
void print (object obj) The function of this method is to return the object.
void print(boolean b) The function of this method is to the boolean value.
printStream append(Char c) The function of this method is to append the represented character for the output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to print stream class.
  • Java.io.Object
  • Java.io.FilteredOutputStream

shape Examples

Usage of void close() method. [c]import java.io.*; public class PrintStream { public static void main(String[] args) { String s = "SP LESSONS"; // creating print stream object PrintStream ps = new PrintStream(System.out); // To print the string ps.println(s); // To closing stream System.out.println("Closing Stream."); // To close the stream ps.close(); } }[/c] Output Following is the result. [c]SP LESSONS Closing Stream. [/c] Usage of void print(boolean b) method. [c]import java.io.*; public class PrintStream { public static void main(String[] args) { boolean bl = false; // creating print stream object PrintStream ps = new PrintStream(System.out); // To print boolean ps.println(true); ps.print(bl); // To flush the stream ps.flush(); } }[/c] Output Following is the result. [c] true false [/c]

Summary

shape Key Points

  • The print stream is the class used to write the data which is formatted, to underlying output stream.
  • The data by print stream is formatted in text including all primitive data types like int, byte, float, double etc.
  • Any bytes of data formatted into text in case of print stream and then it written to output stream.