Site icon TechVidvan

Java PrintStream Class with Examples

java printstream

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:

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

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

This creates a new print stream.

This creates a new print stream.

This creates a new print stream.

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

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

Class methods:

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

This method clears the internal error condition of this stream.

This method closes the stream.

This method flushes the stream.

This method prints a boolean value.

This method prints a character.

This method prints an array of characters.

This method prints a floating point number with double precision.

This method prints a floating point number.

This method prints an integer.

This method prints a long integer.

This method prints the object.

This method prints a string.

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

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

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

This method prints a character and then ends the line.

This method prints a double and then terminates the line.

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

This method prints the integer and then terminates the line.

This method prints a long and then terminates the line.

This method prints the object and then terminates the line.

This method prints the string and then terminates the line.

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

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.

Exit mobile version