Java PrintStream Class with Examples

PrintStream writes raw bytes in the machine’s native character format, and PrintWriter converts bytes to recognized encoding schemes. Therefore, as opposed to the files created by PrintStream, file formats made with PrintWriter are more suitable for different platforms. The PrintWriter class is a method to create any type of data.

Working with PrintStream:

Unlike other output streams, PrintStream converts primitive data (integer, character) to text format instead of bytes. It then writes this formatted data to the output stream.

if a newline character \n is written in the print stream
if the println() method is called.
if a byte array is written to the print stream.

1. Use different output:

currents Creates a file output stream
FileOutputStream file = a new FileOutputStream file for the string;
Initializes a print stream
Print Stream Output = New printStream filename, automaticFlushing;

Here, We have created a print stream to write formatted data to a file that is represented by FileOutputStream AutoFlush is a value option that specifies whether or not an autoflush will be performed.

2. Using filename:

Creates a stream of prints PrintStream output = new PrintStream string file, boolean autoFlushing; Here, We’ve created a stream of prints which will read the formatted data in this specific file. AutoFlush is a required Boolean parameter to determine whether or not you are going to perform autoflush.

Methods of PrintStream:

The Print Stream class is a series of methods that enable us to print data into the output. print() Method Print() will print the required data into an output stream println() will print the data to the output stream with a new line character at the end.

The example:

 print() method with the system class 
class Main { public static void main(String[] args) 
{ 
The string data is "Hello World.";
 System.out.printData; } 
}

printf() method:

The printf() method can be used to print a formatted string. It contains 2 parameters: a formatted string and arguments. For example,

printf("I am %d years old", 25);

Here,

I am %d years old is a formatted string
%d is integer data in a formatted string
25 is an argument
A formatted string contains both text and data. Therefore %d is replaced by 25.

Example:

printf() method using PrintStream
import java.io.PrintStream;

class Main {
    public static void main(String[] args) {

        Try {
            PrintStream output = new PrintStream("output.txt");

            int age = 25;

            output.printf("I am %d years old.", age);
            output.close();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

Output
TechVidvan.txt
I am 25 years old.

Class constructors:

  • PrintStream (file file)

This will create a new print stream with the specified file without automatic line flushing.

  • PrintStream ( File file , String csn )

This creates a new print stream without automatic line flushing with the specified file and character set.

  • PrintStream (OutputStream out)

This creates a new print stream.

  • PrintStream(OutputStream out, boolean autoFlush)

This creates a new print stream.

  • PrintStream(OutputStream out, boolean autoFlush, string encoding)

This creates a new print stream.

  • PrintStream(String fileName)

This creates a new print stream without automatically emptying the line with the specified file name.

  • PrintStream(String fileName, String csn)

This creates a new print stream without automatic line flushing with the specified file name and character set.

Class methods:

  • Attachment PrintStream (CharSequence csq)

This method appends the specified sequence of characters to this output stream.

  • protected void clearError()

This method clears the internal error condition of this stream.

  • void close()

This method closes the stream.

  • void flush()

This method flushes the stream.

  • void print (boolean b)

This method prints a boolean value.

  • invalid print (character c)

This method prints a character.

  • invalid print(char[] s)

This method prints an array of characters.

  • blank print (double d)

This method prints a floating point number with double precision.

  • empty print (float f)

This method prints a floating point number.

  • void print(int i)

This method prints an integer.

  • blank print (long l)

This method prints a long integer.

  • void print(Object obj)

This method prints the object.

  • empty print (string s)

This method prints a string.

  • PrintStream printf(Locale l, Format string, Object… args)

This is a convenient way to write a formatted string to this output stream using the specified format string and arguments.

  • PrintStream printf (string format, object arguments…)

This is a convenient way to write a formatted string to this output stream using the specified format string and arguments.

  • void println()

This method ends the current line by writing a line separator string.

  • void println (char x)

This method prints a character and then ends the line.

  • void println(double x)

This method prints a double and then terminates the line.

  • void println (float x)

This method prints a floating line and then terminates the line.

  • void println(int x)

This method prints the integer and then terminates the line.

  • void println(long x)

This method prints a long and then terminates the line.

  • void println(Object x)

This method prints the object and then terminates the line.

  • void println(string x)

This method prints the string and then terminates the line.

  • void write(byte[] buf, int off, int len)

This method writes only the bytes from the specified byte array starting at offset to this stream.

  • void write(int b)

This method writes the specified byte to this stream.

Conclusion

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well.