Java PrintStream Class

Java PrintStream class

What is Java PrintStream Class?

The Java PrintStream class is part of the java.io package and provides a way to print formatted representations of objects to a text-output stream.

It extends the OutputStream class and has methods such as print(), println(), format(), printf(), and flush(). It can be used to write data to a file or any other type of output stream.

To understand the more about Java PrintStream Class, Read the Complete Article.

Working of PrintStream: 

The PrintStream class in Java works by taking input from various sources such as standard input, files, or network sockets and converting them into a sequence of bytes. It then sends these bytes to the underlying output stream.

The PrintStream class in Java has various methods such as print(), println(), format(), printf(), and flush(), which enable you to format and print data to the output stream. When you use any of these methods, the PrintStream class converts the data into a sequence of bytes and sends them to the output stream. If the output stream is buffered, you may need to call the flush() method to make sure that all data is written to the stream.

You can create a PrintStream object using one of its constructors, which take an OutputStream as a parameter. This allows you to specify the destination of the output stream, such as a file or network socket.

Overall, the PrintStream class provides a convenient way to format and print data to an output stream in Java.

Java PrintStream Class

Advantages of using try-with-resources: 

Methods of PrintStream

MethodsDescription
print()This method prints a given string to the output stream.
println()This method prints a given string to the output stream, followed by a line separator.
printf()These methods allow you to format a string with specified arguments, and print it to the output stream.
flush()This method flushes the output stream, ensuring that any buffered data is written to the underlying output device.
checkError()This method returns a boolean value indicating whether an error has occurred on the output stream.

These methods provide a convenient way to format and print data to an output stream using the PrintStream class in Java.

Example 1: Java Program to print() method with System class

Run
public class Main{
    public static void main(String[] args) {
        System.out.print("Hello");
        System.out.print(", ");
        System.out.print("world!");
    }
}

Output

Hello, world!

Example 2: Java Program to printf() method using PrintStream

Run
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("output.txt");
        PrintStream ps = new PrintStream(fos);
        
        double pi = Math.PI;
        int x = 42;
        String s = "Java";
        
        ps.printf("The value of pi is approximately %.2f\n", pi);
        ps.printf("The answer to everything is %d\n", x);
        ps.printf("Hello, %s!\n", s);
        
        ps.close();
        fos.close();
    }
}

Output

The value of pi is approximately 3.14
The answer to everything is 42
Hello, Java!

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription