IO Information
IO Information
System.out.println("simple message");
System.err.println("error message");
OutputStream
Java application uses an output stream to write data to a
destination; it may be a file, an array, peripheral device or
socket.
InputStream
Java application uses an input stream to read data from a
source; it may be a file, an array, peripheral device or socket.
the working of Java OutputStream and InputStream by the
figure given below.
InputStream Class
The InputStream class provides methods to read bytes from a
file, console or memory.
SN Class Description
1 BufferedInputStream This class provides methods to read bytes from the buffer.
2 ByteArrayInputStream This class provides methods to read bytes from the byte array.
3 DataInputStream This class provides methods to read Java primitive data types.
4 int available () This method returns the number of bytes that are available to read.
5 Void mark(int nBytes) This method is used to mark the current position in the input stream until the
specified nBytes are read.
6 void reset () This method is used to reset the input pointer to the previously set mark.
7 long skip (long nBytes) This method is used to skip the nBytes of the input stream and returns the total
number of bytes that are skipped.
8 void close () This method is used to close the input source. If an attempt is made to read even
after the closing, IOException is thrown by the method.
OutputStream Class
The OutputStream is an abstract class that is used to write 8-bit bytes to
the stream. It is the superclass of all the output stream classes.
SN Class Description
1 BufferedOutputStream This class provides methods to write the bytes to the buffer.
2 ByteArrayOutputStream This class provides methods to write bytes to the byte array.
3 DataOutputStream This class provides methods to write the java primitive data types.
SN Method Description
1 void write (int i) This method is used to write the specified single byte to the output
stream.
2 void write (byte buffer [] ) It is used to write a byte array to the output stream.
3 Void write(bytes buffer[], It is used to write nByte bytes to the output stream from the buffer
int loc, int nBytes) starting at the specified location.
4 void flush () It is used to flush the output stream and writes the pending buffered
bytes.
5 void close () It is used to close the output stream. However, if we try to close the
already closed output stream, the IOException will be thrown by this
method.
CharacterStream Classes in Java
• The java.io package provides CharacterStream classes to overcome
the limitations of ByteStream classes, which can only handle the 8-bit
bytes and is not compatible to work directly with the Unicode
characters. CharacterStream classes are used to work with 16-bit
Unicode characters. They can perform operations on characters, char
arrays and Strings.
• However, the CharacterStream classes are mainly used to read
characters from the source and write them to the destination.
• For this purpose, the CharacterStream classes are divided into two
types of classes, I.e., Reader class and Writer class.
Reader Class
Reader class is used to read the 16-bit characters from the input stream. However, it is an abstract class and can't be
instantiated, but there are various subclasses that inherit the Reader class and override the methods of the Reader class.
All methods of the Reader class throw an IOException. The subclasses of the Reader class are given in the following table.
SN Class Description
1. BufferedReader This class provides methods to read characters from the buffer.
2. CharArrayReader This class provides methods to read characters from the char array.
3. FileReader This class provides methods to read characters from the file.
4. FilterReader This class provides methods to read characters from the underlying
character input stream.
6 PipedReader This class provides methods to read characters from the connected piped
output stream.
SN Method Description
1 int read() This method returns the integral representation of the next character present in the input.
It returns -1 if the end of the input is encountered.
2 int read(char buffer[]) This method is used to read from the specified buffer. It returns the total number of
characters successfully read. It returns -1 if the end of the input is encountered.
3 int read(char buffer[], int This method is used to read the specified nChars from the buffer at the specified location.
loc, int nChars) It returns the total number of characters successfully read.
4 void mark(int nchars) This method is used to mark the current position in the input stream until nChars
characters are read.
5 void reset() This method is used to reset the input pointer to the previous set mark.
6 long skip(long nChars) This method is used to skip the specified nChars characters from the input stream and
returns the number of characters skipped.
7 boolean ready() This method returns a boolean value true if the next request of input is ready. Otherwise,
it returns false.
8 void close() This method is used to close the input stream. However, if the program attempts to access
the input, it generates IOException.
Writer Class
Writer class is used to write 16-bit Unicode characters to the output stream. The
methods of the Writer class generate IOException.
Like Reader class, Writer class is also an abstract class that cannot be instantiated;
therefore, the subclasses of the Writer class are used to write the characters onto
the output stream. The subclasses of the Writer class are given in the below table.
SN Class Description
1 BufferedWriter This class provides methods to write characters to the buffer.
2 FileWriter This class provides methods to write characters to the file.
3 CharArrayWriter This class provides methods to write the characters to the character array.
4 OutpuStreamWriter This class provides methods to convert from bytes to characters.
5 PipedWriter This class provides methods to write the characters to the piped output
stream.
6 StringWriter This class provides methods to write the characters to the string.
To write the characters to the output stream, the Write class provides various methods given in the following table.
SN Method Description
1 void write() This method is used to write the data to the output stream.
2 void write(int i) This method is used to write a single character to the output
stream.
3 Void write(char buffer[]) This method is used to write the array of characters to the
output stream.
4 void write(char buffer [],int loc, This method is used to write the nChars characters to the
int nChars) character array from the specified location.
5 void close () This method is used to close the output stream. However, this
generates the IOException if an attempt is made to write to the
output stream after closing the stream.
6 void flush () This method is used to flush the output stream and writes the
waiting buffered characters.