Class PrintStream in Java

digitization, transformation, binary

Class PrintStream in Java

A PrintStream implements an output stream filter that provides convenient methods for printing types other than byte and arrays of bytes. In addition, the PrintStream overrides many of the InputStream methods so as not to throw an IOException. Instead, an IOException causes an internal flag to be set, which the application can check by a call to the checkError() method. Only the lower 8 bits of any 16-bit quantity is printed to the stream.

PrintStream class is a FilterOutputStream that implements a number of methods for displaying a textual representation of Java primitive data types. The print () methods output a standard textual representation of each data type.

Ads code goes here

The println () methods do the same and follow that representation with a newline. The methods convert various Java primitive types to String representations and then output the resulting string.

When an object is passed to a print () or println (). it is converted to a string by calling its toString () method.

Printstream is the OutputStream type that makes it easiest to output text. As such, it is the most commonly used of the output streams. The System.out variable is a PrintStream.

Special Note: Printstream and its PrintWriter replacement output textual representations of Java data types. Use DataOutputStream to output binary representations of data.

PrintStream uses the system’s default encoding scheme to convert characters to bytes and uses the system’s own specific line separator, rather than the newline character, for separating lines of text. Although this class is not officially deprecated, its constructors are!! and we should use PrintWriter instead of PrintStream in new code.

PrintStream did not handle Unicode characters. Any PrintStream methods that wrote characters only wrote the low eight bits of each character.

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

A PrintStream object is often used to write to a BufferedOutputStream object. Note that you
can specify that the PrintStream be flushed every time it writes the line separator or the newline character by using the constructor that takes a boolean argument.

PrintStream 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 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.

Example

 System.out.println("The value of y"+y); 

The structure of class PrintStream is given as:

public class java.io.PrintStream extends java.io.FilterOutputStream
// Public Constructors
public PrintStream (OutputStream out);
public PrintStream (OutputStream out, boolean autoFlush);
// Public Instance Methods
public boolean checkError ();
public void close();// Overrides FilterOutputStream
public void flush();// Overrides FilterOutputStream
public void print(boolean b);
public void print(char c);
public void print(int i);
public void print(long 1);
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); co
public void println(String x);
public void println(Object x);
public void write(int b);// Overrides FilterOutputStream
public void write(byte[ ] buf, int off, int len);// Overrides FilterOutputStream
// Protected Instance Methods
protected void setError();
}

The details of the PrintStream class is given as:

public PrintStream(OutputStream out); 

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

READ  Learn Constructor in Java in an Easy and Fast Manner

Parameter
out– The output stream to use.

public PrintStream(OutputStream out,boolean autoFlush); 

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

If autoflush is true, every time the PrintStream object writes a newline character or line
separator, it calls its flush() method. Note that this is different than with a PrintWriter
object, which only calls its flush() method when a println() method is called.

Parameter
out – The output stream to use.
autoflush – A boolean value that indicates whether or not the print stream is flushed every time a newline is an output.

public boolean checkError();

public boolean checkError() method flushes any buffered output and returns true if any error has occurred. Once the error flag for a PrintStream object has been set, it is 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.

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.

READ  Abstract Interface Observer in Java

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(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 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 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).

READ  Class FileDescriptor in Java

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).

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 b);

public void write(int b) method writes the lowest eight bits of b to the underlying stream as a byte. 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 byte is written.

Parameter
b – The value to write to the stream.

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

public void write(byte[] buf, int off, int len) method writes the lowest eight bits of each of len bytes 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(b, off, len) for the underlying output stream and catching any exceptions that are thrown. If necessary, the method blocks until the bytes are written.

Parameter
b – An array of bytes to write to the stream.
off – An offset into the byte array.
len – The number of bytes to write.

protected void setError();

protected void setError() method sets the error state of the PrintStream object to true. Any subsequent calls to getError() return true.
Apart from these PrintStream 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 *