Page 361 - Cs_withBlue_J_C11_Flipbook
P. 361
The InputStream Class
The InputStream is an abstract class which means it cannot be instantiated. Various classes inherit the InputStream
class and override its methods. Let us learn about two main classes - FileInputStream and DataInputStream which
inherit the InputStream class. These classes are used to read input from a standard input device like keyboard, mouse,
memory, and any other program.
The FileInputStream Class
The FileInputStream class has methods to read bytes from a file. It is meant for reading streams of raw bytes such as
image data. The following table shows the description of the commonly used methods of the FileInputStream class.
Method Description
close() It closes the current file input stream.
read() It reads a byte of data from this input stream.
The DataInputStream Class
The DataInputStream class has methods to read primitive data types from an underlying input stream. It is not directly
inherited from the InputStream class. It is inherited from the FileInputStream class. The following table shows the
description of the commonly used methods of the DataInputStream class.
Method Description
read() It reads the number of bytes from the input stream.
readBoolean() It reads one input byte and returns true if that byte is nonzero, false if
that byte is zero.
readByte() It reads and returns one input byte.
readChar() It reads two input bytes and returns a char value.
readDouble() It reads eight input bytes and returns a double value.
readFloat() It reads four input bytes and returns a float value.
readInt() It reads four input bytes and returns an int value.
readLong() It reads eight input bytes and returns a long value.
readShort() It reads two input bytes and returns a short value.
readUTF() It reads from the stream in a representation of a Unicode character string.
The OutputStream Class
The OutputStream class is also abstract in nature. So, we cannot create its objects. But the classes derived from it can
write a stream of bytes. Let us learn about two main classes named FileOutputStream and DataOutputStream which
inherit the OutputStream class.
The FileOutputStream Class
The FileOutputStream class has methods to write bytes to a file. The most commonly used methods of the
FileOutputStream class are listed in the following table.
Method Description
close() It closes the current file output stream and releases any system
resources associated with this stream.
write() It writes the specified byte to the file output stream. There are
two overloaded methods for write() method.
359
Basic Input/Output and Data File Handling 359

