Java

PrintWriter Class

“This class offers text-output stream object representations that are formatted for printing. It uses every print technique available in PrintStream. It lacks ways for writing unencoded byte streams, which a program should utilize instead of writing raw bytes. Unlike the PrintStream class, which does automatic flushing anytime a newline character is output, this class only performs flushing whenever the println, printf, or format operations are called. These techniques substitute the platform’s internal line separator with the newline character.

Despite several of its constructors being capable of doing so, this class’s methods never throw I/O exceptions. The client can verify if any errors have been encountered by using the checkError() call.”

Example # 1: Using PrintWriter Class for printf() Method in Java in Ubuntu 20.04

An efficient way to print a formatted string using the given format string and parameters to this writer. This method takes three arguments: locale l, String format, and object args. The “locale” will be used for formatting. No localization is performed if “l” is nill. The “format” is a format string according to the format string syntax, and “args” is a list of arguments that the format constructors in the format string refer to.

Inside our header section, we have included the two java modules for this java program. Then, we constructed the java class as “PrintWriterTest1,” which is public. The public main method is established inside the java class. The code was then added to the main method after that. There, we have declared the string variable as “str” and initialized it with the string value “of”. We invoked the Java PrintWriter class that creates an object “pw”. The “pw” object is set with the new keyword for the PrintWriter method and takes the “system.out” command as an input. The above-specified string is printed with the “locale” parameter inside the printf method. The flush() method flushed the stream here.

With the PrintWrite java class, we have printed the above string in the terminal:

Example # 2: Using PrintWriter Class for the print() Method With two Parameters in Java in Ubuntu 20.04

A quick technique of using the supplied parameters and format string to write a formatted string to the printf method. The two parameters are passed inside the printf method, which are “format” and “args”.

In our java class “PrintWriteTest2”, we have defined the main method. Inside the static void main method, we have created the variable “st” of the data type string and set the string value “to” for the variable. Then, we called the PrinterWriter method and passed the “system.out” method for the java class “PrinterWriter”. With the instance of the “PrintWriter”, we have displayed the new string and the string which is initialized in the variable “st”.

The new formatted string and the string defined inside the variable with the “%s” operator are displayed here:

Example # 3: Using PrintWriter Class for println() Method in Java in Ubuntu 20.04

The main drawback to the print() function is that it writes all of the objects on one line; however, println() fixes this. When the println() method detects the end of the line, it breaks the line after printing the objects.

To the java class “PrintWriter3” main method, we have assigned the variable “myStr” of the data type “String”. Then, this variable is initialized with the string “Linux Articles”. After that, we created the instance “p” for the java class “PrintWriter”. We have called the PrintWriter instance with the method println and passed the “%s” symbol that represents the string with the variable “myStr,” where the string is defined.

The formatted string is now printed by the java compiler as follows:

Example # 4: Using PrintWriter Class for write() Method in Java in Ubuntu 20.04

This technique only outputs a subset of the characters in an array. It takes the parameters (str, int off, arr_len). Here, “str” is a String, where “off” stands for the Offset from which to begin writing characters, and “arr_len” stands for the total amount of characters to be written.

We have established the java class “PrintWriter4” and invoked the class main method. There, we have set the variable “str,” which has the string value “Hello Geeks”. Then, we implemented the try-catch block. In the try block, we have created the PrintWriter object “pw”. Then, we invoked the write method with the instance “pw” of the PrintWriter class. The write() method prints the substrings from the string within the specified offset and length values. If any error occurs in the try body, the catch block will trace the error.

The string is displayed by the Java PrintWriter class that provides the write method.

Example # 5: Using PrintWriter Class for setError() Method in Java in Ubuntu 20.04

We used PrintWrite’s class method strError(), which establishes the stream’s error state as true.

The java class “PrintWriter5” is constructed here, which extends the PrintWriter class. After that, we created the parent class “PrintWriter5” constructor by utilizing the super keyword. The super keyword takes the object “output” for the “OutputStream” class. Then, we have a main method where the character array is defined that has ASCII values of the alphabets. The instance “p” of the class “PrintWriter” is applied using the write method. The write method writes the characters as “1-3”. The stream will be flushed by the flush method, and the PrintWriter invoked the other method setError for the internal exception that occurred here.

The output displayed the characters against the number after the write operation.

Conclusion

This is a Java PrintWriter user guide. Here, we go over the Java PrintWriter’s introduction, constructors, methods, and samples for better comprehension. The built-in PrintWriter class in Java is used to print an object in a text output stream with its formatted representation. It is defined in the “java.io.PrintWriter” package. All print methods, except for those that write raw bytes, are implemented by the PrintWriter class as PrintStream. It is handy to create reports that combine text and numbers, as this is what the PrintWriter class was designed to do.

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.