PrintWriter Class in Java

digitization, transformation, binary

PrintWriter Class in Java

PrintWriter class is a character output stream that implements a number of print() and println() methods that output textual representations of primitive values and objects.

When we create a PrintWriter object, we need to specify a character or byte output stream that it should write its characters to, and also optionally specify whether the PrintWriter stream should be automatically flushed whenever println() is called. If we specify a byte output stream as the destination, the PrintWriter() constructor automatically creates the necessary OutputStreamWriter object to convert characters to bytes using the default encoding.

Ads code goes here

PrintWriter implements the normal write(), flush(), and close() methods that all Writer subclasses do. It is more common to use the higher-level print() and println() methods, each of which converts its argument to a string before outputting it. println() has the additional behavior of terminating the line (and optionally flushing the buffer) after printing its argument.

The methods of PrintWriter never throw exceptions. Instead, when errors occur, they set an internal flag that we can check by calling checkError(). checkError() first flushes the internal stream and then returns true if an exception has occurred while writing values to that stream. Once an error has occurred on a PrintWriter object, all subsequent calls to checkError() return true; there is no way to reset the error flag.

PrintWriter is the character stream analog to PrintStream, which it supersedes. We can usually trivially replace any PrintStream objects in a program with PrintWriter objects. This is particularly important for internationalized programs. The only valid remaining use for the PrintStream class is for the System.out and System.err standard output streams.

The PrintWriter class provides support for writing string representations of primitive data types and objects to an underlying output stream. PrintWriter uses the system’s default encoding scheme to convert characters to bytes. PrintWriter also uses the system’s own specific line separator, rather than the newline character, for separating lines of text. This line separator is equivalent to the value returned by:

System.getProperty(“line.separator”)

A PrintWriter object can be created using a Writer object or an OutputStream object as its
underlying stream. When a PrintWriter is created using an OutputStream, the constructor
creates the intermediate OutputStreamWriter that handles the conversion of characters to bytes using the default character encoding.

All of the methods of PrintWriter that write multiple times to the underlying output stream handle synchronization internally, so that PrintWriter objects are thread-safe.

A PrintWriter object is often used to write to a BufferedWriter object. Note that you can specify that the PrintWriter should be flushed every time a println() method is called by using a constructor that takes a boolean argument.

PrintWriter objects are often used to report errors. For this reason, the methods of this class do not throw exceptions. Instead, the methods catch any exceptions thrown by any downstream OutputStream or Writer objects and set an internal flag, so that the object can remember that a problem occurred. We can query the internal flag by calling the checkError() method.

The class structure for PrintWriter is given as

public class java.io.PrintWriter extends java.io.Writer{
// Public Constructors
public PrintWriter(Writer out);
public PrintWriter(Writer out, boolean autoFlush);
public PrintWriter(OutputStream out);
public PrintWriter(OutputStream out, boolean autoFlush);
// Public Instance Methods
public boolean checkError();
public void close(); // Defines Writer
public void flush(); // Defines Writer
public void print(boolean b);
public void print(char c);
public void print(int i);
public void print(long l);
public void print(float f);
public void print(double d);
public void print(char[] s);
public void print(String s);
public void print(Object obj);
public void println();
public void println(boolean x);
public void println(char x);
public void println(int x);
public void println(long x);
public void println(float x);
public void println(double x);
public void println(char[] x);
public void println(String x);
public void println(Object x);
public void write(int c); // Overrides Writer
public void write(char[] buf, int off, int len); // Defines Writer
public void write(char[] buf); // Overrides Writer
public void write(String s, int off, int len); // Overrides Writer
public void write(String s); // Overrides Writer
// Protected Instance Methods
protected void setError();
}

The details of the PrintWriter is given as:

public PrintWriter(OutputStream out);

public PrintWriter(OutputStream out) constructor creates a PrintWriter object that sends output to the given OutputStream.

READ  Class PrintStream in Java

The constructor creates the intermediate OutputStreamWriter that converts characters to
bytes using the default character encoding.
Parameter
out-The output stream to use.

public PrintWriter(OutputStream out, boolean autoFlush);

public PrintWriter(OutputStream out, boolean autoFlush) constructor creates a PrintWriter object that sends output to the given OutputStream.

The constructor creates the intermediate OutputStreamWriter that converts characters to
bytes using the default character encoding.

If autoFlush is true, every time a println() method is called, the PrintWriter object calls its flush() method. This behavior is different from that of a PrintStream object, which calls its flush() method each time a line separator or newline character is written.

Parameter
out – The output stream to use.
autoFlush – A boolean value that indicates whether or not the print stream is flushed every time a println() method is called.

public PrintWriter(Writer out);

public PrintWriter(Writer out) constructor creates a PrintWriter object that sends output to the given Writer.

Parameter
out – The output stream to use.

public PrintWriter(Writer out, boolean autoFlush);

public PrintWriter(Writer out, boolean autoFlush) constructor creates a PrintWriter object that sends output to the given Writer.

If autoFlush is true, every time a println() method is called, the PrintWriter object calls its flush() method. Note that this behavior is different from that of a PrintStream object, which calls its flush() method every time a newline character or line separator is written.

Parameter
out – The output stream to use.
autoFlush – A boolean value that indicates whether or not the print stream is flushed every time a println() method is called.

public boolean checkError();

public boolean checkError() method flushes any buffered output and returns true if an error occurs. Once the error flag for a PrintWriter object is set, it’s never cleared.
This method returns true if any error has occurred; false otherwise.

public void close();

public void close() method closes this print stream and releases any resources associated with the object. The method does this by calling the close() method of the underlying output stream and catching any exceptions that are thrown.

public void flush();

public void flush() method flushes this print stream, forcing any bytes that may be buffered to be written to the underlying output stream. The method does this by calling the flush() method of the underlying output stream and catching any exceptions that are thrown.

public void print(boolean b);

public void print(boolean b) method writes “true” to the underlying output stream if b is true; otherwise, it writes “false”.
Parameter
b – The boolean value to print.

public void print(char c);

public void print(char c) method writes the given character to the underlying output stream.

READ  How Java is Platform Independent Language?

Parameter
c – The char value to print.

public void print(char[] s);

public void print(char[] s) method writes the characters in the given array to the underlying output stream.

Parameter
s – The char array to print.

public void print(double d);

public void print(double d) method writes a string representation of the given double value to the underlying output stream. The string representation is identical to the one returned by calling Double.toString(d).

Parameter
d – The double value to print.

public void print(float f);

public void print(float f) method writes a string representation of the given float value to the underlying output stream. The string representation is identical to the one returned by calling
Float.toString(f).

Parameter
f – The float value to print.

public void print(int i);

public void print(int i) method writes a string representation of the given int value to the underlying output stream.

The string representation is identical to the one returned by calling Integer.toString(i).
Parameter
i – The int value to print.

public void print(long l);

public void print(long l) method writes a string representation of the given long value to the underlying output stream. The string representation is identical to the one returned by calling
Long.toString(l).

Parameter
l – The long value to print.

public void print(Object obj);

public void print(Object obj) method writes the string representation of the given Object to the underlying output stream.

The string representation is that returned by calling the toString() method of Object.

Parameter
obj – The Object to print.

public void print(String s);

public void print(String s) method writes the given String to the underlying output stream. If String is null, the method writes “null”.

Parameter
s – The String to print.

public void println();

public void println() method writes a line separator to the underlying output stream.

public void println(boolean b);

public void println(boolean b) method writes “true” to the underlying output stream if b is true, otherwise, it writes “false”. In either case, the string is followed by a line separator.

Parameter
b – The boolean value to print.

public void println(char c);

public void println(char c) method writes the given character, followed by a line separator, to the underlying output stream.

Parameter
c – The char value to print.

public void println(char[] s);

public void println(char[] s) method writes the characters in the given array, followed by a line separator, to the underlying output stream.

Parameter
s – The char array to print.

public void println(double d);

public void println(double d) method writes a string representation of the given double value, followed by a line separator, to the underlying output stream. The string representation is identical to the one returned by calling Double.toString(d).

Parameter
d – The double value to print.

public void println(float f);

public void println(float f) method writes a string representation of the given float value, followed by a line separator, to the underlying output stream. The string representation is identical to the one returned by calling Float.toString(f).

Parameter
f – The float value to print.

public void println(int i);

public void println(int i) method writes a string representation of the given int value, followed by a line separator, to the underlying output stream. The string representation is identical to the one returned by calling Integer.toString(i).

Parameter
i – The int value to print.

public void println(long l);

public void println(long l) method writes a string representation of the given long value, followed by a line separator, to the underlying output stream. The string representation is identical to the one returned by calling Long.toString(l).

READ  Abstract Class WindowAdapter in Java

Parameter
l – The long value to print.

public void println(Object obj);

public void println(Object obj) method writes the string representation of the given Object, followed by a line separator, to the underlying output stream. The string representation is that returned by calling the toString() method of Object.

Parameter
obj – The Object to print.

public void println(String s);

public void println(String s) method writes the given String, followed by a line separator, to the underlying output stream. If String is null, the method writes “null” followed by a line separator.

Parameter
s – The String to print.

public void write(int c);

public void write(int c) method writes the character specified by the lowest two bytes of the given integer c to the underlying stream. The method does this by calling the write() method of the underlying output stream and catching any exceptions that are thrown. If necessary, the method blocks until the character are written.

Parameter
c– The value to write to the stream.

public void write(char[] buf);

public void write(char[] buf) method writes the given array of characters to the underlying output stream. The method does this by calling write(buf, 0, buf.length) for the underlying output stream and catching any exceptions that are thrown. If necessary, the method blocks until the characters are written.

Parameter
buf – An array of characters to write to the stream.

public void write(char[] buf, int off, int len);

public void write(char[] buf, int off, int len) method writes len characters from the given array, starting off elements from the beginning of the array, to the underlying output stream. The method does this by calling write(buf, off, len) for the underlying output stream and catching any exceptions that are thrown. If necessary, the method blocks until the characters are written.

Parameter
buf – An array of characters to write to the stream.
off – An offset into the array.
len – The number of characters to write.

public void write(String s);

public void write(String s) method writes the given String to the underlying output stream. The method does this by calling write(s, 0, s.length) for the underlying output stream and catching any exceptions that are thrown. If necessary, the method blocks until the String is written.

Parameter
s – A String to write to the stream.

public void write(String s, int off, int len);

public void write(String s, int off, int len) method writes len characters from the given String, starting off elements from the beginning of the string, to the underlying output stream. The method does this by calling write(s, off, len) for the underlying output stream and catching any exceptions that are thrown. If necessary, the method blocks until the characters of the String are written.

Parameter
s – A String to write to the stream.
off – An offset into the string.
len – The number of characters to write.

protected void setError();

protected void setError() method sets the error state of the PrintWriter object to true. Any subsequent calls to getError() will return true.
Apart from these PrintWriter class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *