Java

Java PrintStream

“A PrintStream enhances the capability of another output stream by enabling convenient printing of representations of diverse data values. PrintStreams don’t throw IOExceptions as other output streams do; instead, they only raise an inner flag that can be checked using the checkError method when something unusual happens. Each character that a PrintStream prints is converted into a set of bytes using the platform’s predefined character encoding. When writing characters instead of bytes are necessary, the PrintWriter interface should be used.”

Important Features of PrintStream Class in Java

  1. Any data value, primitive, or object can be printed using the methods provided by PrintStream in the proper printing format.
  2. As long as data is being written to an associated output stream, its methods never produce an IOException. If a method call raises an IOException error, it creates an internal flag as opposed to alerting the caller explicitly.
  3. Additionally, auto flushing is a feature of the PrintStream class. To automatically flush the contents that have been written to an output stream, PrintStream introduces an auto-flush capability. This means that under one of the following circumstances, it compels to write the output stream of all of the data to the destination:
  • if the print stream contains the character “\n” (newline)
  • Upon calling the println() method
  • if a collection of bytes is printed in the stream

How to Use PrintStream Class in Java in Ubuntu 20.04

Data printing to the output is made possible via the PrintStream class, which offers many methods to conveniently print representations of different data values. Before creating a PrintStream, the java.io.PrintStream module must be imported. This is how the print stream is created after we load the package. We will explore a few methods of PrintStream class with the java compilation.

Example 1: Using the PrintStream Class Method Println() in Java in Ubuntu 20.04

It is a better iteration of the print() technique. On the console, text was also once displayed by it. It belongs to the PrintStream class and is an overloaded method. As an argument, it takes a string. Once the entire statement has been printed, the pointer is moved to the head of the succeeding line. The fundamental distinction between println() and print() is this.

We established a class, “PrintStream1,” which invokes the main method. Inside the “PrintStream1” class-main, we have created the FileOutputStream object “FOS” that is linked with the file “file.txt”. After that, the object “FOS” is passed inside the PrintStream class as an input. With the instance “p” of the PrintStream, we have utilized the println function. The println method prints the specified integer value and string values inside the file “file.txt”.

First, we have compiled our code with the java class name “PrintStream1”.

After that, we have opened the file “file.txt,” where the println method prints the integer and string values as follows:

Example 2: Using the PrintStream Class Method Printf() in Java in Ubuntu 20.04

The prepared string can be printed using the printf() method. Arguments and a formatted string are two of its included parameters. Both text and data are included in the formatted string. And the data within the prepared string is replaced by the parameters.

We have a class, “PrintStream2,” and we have created the main method in that class. After that, we called the try-catch block. In the try block, we have defined the print stream as a “file”. The PrintStream then takes the file “File.txt” as an input. The variable is labeled as “DOB” and given an integer value. Here, this integer value “25” is an argument for the printf() method provided by the PrintStream class. The statement given in the printf() method is a formatted string.

When the code is compiled, the above print content is inserted into the file “File.txt”.

Example 3: Using the PrintStream Class Method Print() in Java in Ubuntu 20.04

For text to appear on the console, use the print() method. It is a PrintStream class method that has been overloaded. It’s possible to pass in a string as a parameter. The statement has been printed, but the cursor is still on that line. It also functions if no parameters are parsed.

Within the main method of the class “PrintStream3”, we have initially declared the variable “text,” which has the string as the data type “String” is set for this variable. Then, we used the try-catch block. There, we have a “result” object for the creation of the print stream, and the print stream has the file “File.txt”. We have introduced another method, “print,” from PrintStream. The print method took the “text” variable and inserted it into the file “File.txt”.

When the file “File.txt” is opened, it is loaded with the string specified above.

Example 4: Using the PrintStream Class Method Write() in Java in Ubuntu 20.04

There, we have a different method that the PrintStream class offers, known as Writes(). Writes() “len” bytes beginning at offset “off” in the given byte array to this stream. The “len” and the “off” are the parameters this method takes.

For the main method of the class “PrintStream4”, we have provided the variable “ch” of type “bytes”. We have set the value “75” for the “ch” variable. Then, we created the object as “ps” for the print stream and called the “system.out”. After that, we employed the write() method and passed the variable “ch” to it. Here, the given byte is written to this output stream via the method write.

The write() method displayed the output of the specified byte.

Conclusion

Our knowledge of Java’s PrintStream class was expanded upon in this guide. There are several techniques for writing data to other streams provided by this class. Primitive data is transformed into text format by this class, which then outputs the result to the output stream. Data is printed into an OutputStream that is managed by the subclass of FilterOutputStream, known as PrintStream. It is also regarded as a tool for enhancing the functionality of this OutputStream.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.