Category: 09. Java.io package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTu7QxNc2xSxxckK_X5MKxqPL6enQiDOyfZ2w&s

  • PushbackInputStream 

    Introduction

    The Java.io.PushbackInputStream class adds functionality to another input stream, namely the ability to “push back” or “unread” one byte.

    Class declaration

    Following is the declaration for Java.io.PushbackInputStream class −

    public class PushbackInputStream
       extends FilterInputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PushbackInputStream class −

    • protected byte[] buf − This is the pushback buffer.
    • protected int pos − This is the position within the pushback buffer from which the next byte will be read.
    • protected InputStream in − This is the input stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1PushbackInputStream(InputStream in)This creates a PushbackInputStream and saves its argument, the input stream in, for later use.
    2PushbackInputStream(InputStream in, int size)This creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
    2void close()This method closes this input stream and releases any system resources associated with the stream.
    3void mark(int readlimit)This method marks the current position in this input stream.
    4boolean markSupported()This method tests if this input stream supports the mark and reset methods, which it does not.
    5int read()This method reads the next byte of data from this input stream.
    6int read(byte[] b, int off, int len)This method reads up to len bytes of data from this input stream into an array of bytes.
    7void reset()This method repositions this stream to the position at the time the mark method was last called on this input stream.
    8long skip(long n)This method skips over and discards n bytes of data from this input stream.
    9void unread(byte[] b)This method pushes back an array of bytes by copying it to the front of the pushback buffer.
    10void unread(byte[] b, int off, int len)This method pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.
    11void unread(int b)This method pushes back a byte by copying it to the front of the pushback buffer.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.FilterInputStream
    • Java.io.Object
  • PrintWriter 

    Introduction

    The Java.io.PrintWriter class prints formatted representations of objects to a text-output stream.

    Class declaration

    Following is the declaration for Java.io.PrintWriter class −

    public class PrintWriter
       extends Writer
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PrintWriter class −

    • protected Writer out − This is the character-output stream of this PrintWriter.
    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1PrintWriter(File file)This creates a new PrintWriter, without automatic line flushing, with the specified file.
    2PrintWriter(File file, String csn)This creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
    3PrintWriter(OutputStream out)This creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
    4PrintWriter(OutputStream out, boolean autoFlush)This creates a new PrintWriter from an existing OutputStream.
    5PrintWriter(String fileName)This creates a new PrintWriter, without automatic line flushing, with the specified file name.
    6PrintWriter(String fileName, String csn)This creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
    7PrintWriter(Writer out)This creates a new PrintWriter, without automatic line flushing.
    8PrintWriter(Writer out, boolean autoFlush)This creates a new PrintWriter.

    Class methods

    Sr.No.Method & Description
    1PrintWriter append(char c)This method appends the specified character to this writer.
    2PrintWriter append(CharSequence csq)This method appends the specified character sequence to this writer.
    3PrintWriter append(CharSequence csq, int start, int end)This method appends a subsequence of the specified character sequence to this writer.
    4boolean checkError()This method flushes the stream if it’s not closed and checks its error state.
    5protected void clearError()This method Clears the error state of this stream.
    6void close()This method Closes the stream and releases any system resources associated with it.
    7void flush()This method Flushes the stream.
    8PrintWriter format(Locale l, String format, Object… args)This method writes a formatted string to this writer using the specified format string and arguments.
    9PrintWriter format(String format, Object… args)This method writes a formatted string to this writer using the specified format string and arguments.
    10void print(boolean b)This method prints a boolean value.
    11void print(char c)This method prints a character.
    12void print(char[] s)This method Prints an array of characters.
    13void print(double d)This method Prints a double-precision floating-point number.
    14void print(float f)This method prints a floating-point number.
    15void print(int i)This method prints an integer.
    16void print(long l)This method prints a long integer.
    17void print(Object obj)This method prints an object.
    18void print(String s)This method prints a string.
    19PrintWriter printf(Locale l, String format, Object… args)This is a convenience method to write a formatted string to this writer using the specified format string and arguments.
    20PrintWriter printf(String format, Object… args)This is a convenience method to write a formatted string to this writer using the specified format string and arguments.
    21void println()This method terminates the current line by writing the line separator string.
    22void println(boolean x)This method prints a boolean value and then terminates the line.
    23void println(char x)This method prints a character and then terminates the line.
    24void println(char[] x)This method prints an array of characters and then terminates the line.
    25void println(double x)This method prints a double-precision floating-point number and then terminates the line.
    26void println(float x)This method prints a floating-point number and then terminates the line.
    27void println(int x)This method prints an integer and then terminates the line.
    28void println(long x)This method prints a long integer and then terminates the line.
    29void println(Object x)This method prints an Object and then terminates the line.
    30void println(String x)This method prints a String and then terminates the line.
    31protected void setError()This method indicates that an error has occurred.
    32void write(char[] buf)This method writes an array of characters.
    33void write(char[] buf, int off, int len)This method writes a portion of an array of characters.
    34void write(int c)This methodWrites a single character.
    35void write(String s)This method writes a string.
    36void write(String s, int off, int len)This method writes a portion of a string.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • PrintStream 

    Introduction

    The Java.io.PrintStream class adds functionality to another output stream, the ability to print representations of various data values conveniently.

    Class declaration

    Following is the declaration for Java.io.PrintStream class −

    public class PrintStream
       extends FilterOutputStream
    
      implements Appendable, Closeable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PrintStream class −

    • protected OutputStream out − This is the output stream to be filtered.

    Class constructors

    Sr.No.Constructor & Description
    1PrintStream(File file)This creates a new print stream, without automatic line flushing, with the specified file.
    2PrintStream(File file, String csn)This creates a new print stream, without automatic line flushing, with the specified file and charset.
    3PrintStream(OutputStream out)This creates a new print stream.
    4PrintStream(OutputStream out, boolean autoFlush)This creates a new print stream.
    5PrintStream(OutputStream out, boolean autoFlush, String encoding)This creates a new print stream.
    6PrintStream(String fileName)This creates a new print stream, without automatic line flushing, with the specified file name.
    7PrintStream(String fileName, String csn)This creates a new print stream, without automatic line flushing, with the specified file name and charset.

    Class methods

    Sr.No.Method & Description
    1PrintStream append(char c)This method appends the specified character to this output stream.
    2PrintStream append(CharSequence csq)This method appends the specified character sequence to this output stream.
    3PrintStream append(CharSequence csq, int start, int end)This method appends a subsequence of the specified character sequence to this output stream.
    4boolean checkError()This method flushes the stream and checks its error state.
    5protected void clearError()This method clears the internal error state of this stream.
    6void close()This method closes the stream.
    7void flush()This method flushes the stream.
    8PrintStream format(Locale l, String format, Object… args)This method writes a formatted string to this output stream using the specified format string and arguments.
    9PrintStream format(String format, Object… args)This method writes a formatted string to this output stream using the specified format string and arguments.
    10void print(boolean b)This method prints a boolean value.
    11void print(char c)This method prints a character.
    12void print(char[] s)This method prints an array of characters.
    13void print(double d)This method prints a double-precision floating-point number.
    14void print(float f)This method prints a floating-point number.
    15void print(int i)This method prints an integer.
    16void print(long l)This method prints a long integer.
    17void print(Object obj)This method prints an object.
    18void print(String s)This method Prints a string.
    19PrintStream printf(Locale l, String format, Object… args)This is a convenience method to write a formatted string to this output stream using the specified format string and arguments.
    20PrintStream printf(String format, Object… args)This is a convenience method to write a formatted string to this output stream using the specified format string and arguments.
    21void println()This method terminates the current line by writing the line separator string.
    22void println(boolean x)This method prints a boolean and then terminate the line.
    23void println(char x)This method prints a character and then terminate the line.
    24void println(char[] x)This method prints an array of characters and then terminate the line.
    25void println(double x)This method prints a double and then terminate the line.
    26void println(float x)This method Prints a float and then terminate the line.
    27void println(int x)This method prints an integer and then terminate the line.
    28void println(long x)This method prints a long and then terminate the line.
    29void println(Object x)This method prints an Object and then terminate the line.
    30void println(String x)This method prints a String and then terminate the line.
    31protected void setError()This method sets the error state of the stream to true.
    32void write(byte[] buf, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this stream.
    33void write(int b)This method writes the specified byte to this stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.FilterOutputStream
    • Java.io.Object
  • PipedWriter 

    Introduction

    The Java.io.PipedWriter class is piped character-output streams.

    Class declaration

    Following is the declaration for Java.io.PipedWriter class −

    public class PipedWriter
       extends Writer
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PipedWriter class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1PipedWriter()This creates a piped writer that is not yet connected to a piped reader.
    2PipedWriter(PipedReader snk)This creates a piped writer connected to the specified piped reader.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this piped output stream and releases any system resources associated with this stream.
    2void connect(PipedReader snk)This method Connects this piped writer to a receiver.
    3void flush()This method flushes this output stream and forces any buffered output characters to be written out.
    4void write(char[] cbuf, int off, int len)This method writes len characters from the specified character array starting at offset off to this piped output stream.
    5void write(int c)This method writes the specified char to the piped output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Writer
    • Java.io.Object
  • PipedReader 

    Introduction

    The Java.io.PipedReader class is piped character-input streams.

    Class declaration

    Following is the declaration for Java.io.PipedReader class −

    public class PipedReader
       extends Reader
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PipedReader class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1PipedReader()This creates a PipedReader so that it is not yet connected.
    2PipedReader(int pipeSize)This creates a PipedReader so that it is not yet connected and uses the specified pipe size for the pipe’s buffer.
    3PipedReader(PipedWriter src)This creates a PipedReader so that it is connected to the piped writer src.
    4PipedReader(PipedWriter src, int pipeSize)This creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe’s buffer.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this piped stream and releases any system resources associated with the stream.
    2void connect(PipedWriter src)This method causes this piped reader to be connected to the piped writer src.
    3int read()This method reads the next character of data from this piped stream.
    4int read(char[] cbuf, int off, int len)This method reads up to len characters of data from this piped stream into an array of characters.
    5boolean ready()This method tell whether this stream is ready to be read.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Reader
    • Java.io.Object
  • PipedOutputStream 

    Introduction

    The Java.io.PipedOutputStream class is a piped output stream that can be connected to a piped input stream to create a communications pipe.Following are the important points about PipedOutputStream −

    • The piped output stream is the sending end of the pipe.
    • Attempting to use both objects from a single thread is not recommended as it may deadlock the thread.
    • Data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread.
    • The pipe is said to be broken if a thread that was reading data bytes from the connected piped input stream is no longer alive.

    Class declaration

    Following is the declaration for Java.io.PipedOutputStream class −

    public class PipedOutputStream
       extends OutputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1PipedOutputStream()This creates a piped output stream that is not yet connected to a piped input stream.
    2PipedOutputStream(PipedInputStream snk)This creates a piped output stream connected to the specified piped input stream.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this piped output stream and releases any system resources associated with this stream.
    2void connect(PipedInputStream snk)This method connects this piped output stream to a receiver.
    3void flush()This method flushes this output stream and forces any buffered output bytes to be written out.
    4void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this piped output stream.
    5void write(int b)This method writes the specified byte to the piped output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.OutputStream
    • Java.io.Object
  • PipedInputStream 

    Introduction

    The Java.io.PipedInputStream class is a piped input stream that can be connected to a piped output stream, the piped input stream then provides whatever data bytes are written to the piped output stream.Following are the important points about PipedInputStream −

    • The piped input stream contains a buffer, decoupling read operations from write operations, within limits.
    • Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.
    • A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.

    Class declaration

    Following is the declaration for Java.io.PipedInputStream class −

    public class PipedInputStream
       extends InputStream
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.PipedInputStream class −

    • protected byte[] buffer − This is the circular buffer into which incoming data is placed.
    • protected int in − This is the index of the position in the circular buffer at which the next byte of data will be stored when received from the connected piped output stream.
    • protected int out − This is the index of the position in the circular buffer at which the next byte of data will be read by this piped input stream.
    • protected static int PIPE_SIZE − This is the default size of the pipe’s circular input buffer.

    Class constructors

    Sr.No.Constructor & Description
    1PipedInputStream()This creates a PipedInputStream so that it is not yet connected.
    2PipedInputStream(int pipeSize)This creates a PipedInputStream so that it is not yet connected and uses the specified pipe size for the pipe’s buffer.
    3PipedInputStream(PipedOutputStream src)This creates a PipedInputStream so that it is connected to the piped output stream src.
    4PipedInputStream(PipedOutputStream src, int pipeSize)This creates a PipedInputStream so that it is connected to the piped output stream src and uses the specified pipe size for the pipe’s buffer.

    Class methods

    Sr.No.Method & Description
    1int available()This method returns the number of bytes that can be read from this input stream without blocking.
    2void close()This method closes this piped input stream and releases any system resources associated with the stream.
    3void connect(PipedOutputStream src)This method causes this piped input stream to be connected to the piped output stream src.
    4int read()This method reads the next byte of data from this piped input stream.
    5int read(byte[] b, int off, int len)This method reads up to len bytes of data from this piped input stream into an array of bytes.
    6protected void receive(int b)This method receives a byte of data.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.InputStream
    • Java.io.Object
  • OutputStreamWriter 

    Introduction

    The Java.io.OutputStreamWriter class is a bridge from character streams to byte streams. Characters written to it are encoded into bytes using a specified charset.

    Class declaration

    Following is the declaration for Java.io.OutputStreamWriter class −

    public class OutputStreamWriter
       extends Writer
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for Java.io.OutputStreamWriter class −

    • protected Object lock − This is the object used to synchronize operations on this stream.

    Class constructors

    Sr.No.Constructor & Description
    1OutputStreamWriter(OutputStream out)This creates an OutputStreamWriter that uses the default character encoding.
    2OutputStreamWriter(OutputStream out, Charset cs)This creates an OutputStreamWriter that uses the given charset.
    3OutputStreamWriter(OutputStream out, CharsetEncoder enc)This creates an OutputStreamWriter that uses the given charset encoder.
    4OutputStreamWriter(OutputStream out, String charsetName)This creates an OutputStreamWriter that uses the named charset.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes the stream, flushing it first.
    2void flush()This method flushes the stream.
    3String getEncoding()This method returns the name of the character encoding being used by this stream.
    4void write(char[] cbuf, int off, int len)This method writes a portion of an array of characters.
    5void write(int c)This method writes a single character.
    6void write(String str, int off, int len)This method writes a portion of a string.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Writer
    • Java.io.Object
  • OutputStream 

    Introduction

    The Java.io.OutputStream class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output.

    Class declaration

    Following is the declaration for Java.io.OutputStream class −

    public abstract class OutputStream
       extends Object
    
      implements Closeable, Flushable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1OutputStream()Single Constructor.

    Class methods

    Sr.No.Method & Description
    1void close()This method closes this output stream and releases any system resources associated with this stream.
    2void flush()This method flushes this output stream and forces any buffered output bytes to be written out.
    3void write(byte[] b)This method writes b.length bytes from the specified byte array to this output stream.
    4void write(byte[] b, int off, int len)This method writes len bytes from the specified byte array starting at offset off to this output stream.
    5abstract void write(int b)This method writes the specified byte to this output stream.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object
  • ObjectStreamField 

    Introduction

    The Java.io.ObjectStreamField class is a description of a Serializable field from a Serializable class. An array of ObjectStreamFields is used to declare the Serializable fields of a class.

    Class declaration

    Following is the declaration for Java.io.ObjectStreamField class −

    public class ObjectStreamField
       extends Object
    
      implements Comparable<Object>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1ObjectStreamField(String name, Class<?> type)This creates a Serializable field with the specified type.
    2ObjectStreamField(String name, Class<?> type, boolean unshared)This creates an ObjectStreamField representing a serializable field with the given name and type.

    Class methods

    Sr.No.Method & Description
    1int compareTo(Object obj)This method compares this field with another ObjectStreamField.
    2String getName()This method gets the name of this field.
    3int getOffset()This method returns the offset of field within instance data.
    4Class<?> getType()This method gets the type of the field.
    5char getTypeCode()This method returns character encoding of field type.
    6String getTypeString()This method returns the JVM type signature.
    7boolean isPrimitive()This method returns true if this field has a primitive type.
    8boolean isUnshared()This method returns boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.
    9protected void setOffset(int offset)This method returns offset within instance data.
    10String toString()This method returns a string that describes this field.

    Methods inherited

    This class inherits methods from the following classes −

    • Java.io.Object