You are on page 1of 5

IO Streams

(java.io)

Need of this chapter:


 To read and write data to file permanently.
 To read data from the keyboard and write data to the console.
 To understand internal functionalities of System.in, System.out, and
System.err.

 As part of IO Streams chapter, we are learning about all predefined classes given in
“java.io” package.
 File IO or File Handling is a part of IO Streams. By using classes in java.io, we not only
read and write to the file, but also, we can read data from the keyboard and write data
to the console.
 Because the program is executing in RAM(not in Hard Disk), the values stored inside a
variable and an object are available only during the execution of the program. Once the
program execution completes and the control comes out of the method, the memory is
destroyed and the values are lost.
 If we want to store the data permanently, we must store it permanently inside a hard
disk i.e., inside a file.
 But in real-time projects, we don’t store data in a file. We store the data in a Data Base.
 If we want to store small amount of data and less concerned about security, we use
files. If we want to store large amount of data with security, we use Data Base.
Note: To store data in files and DBs, SUN has given in-built API. We all need to do is creating
the particular class object and calling methods on that object for storing and reading data
from the persistent media.
 To write and read data in a file, we classes of java.io package.
 To write and read data in Data Base, we use JDBC API.
 To write and read data to remote computer through network, we use Network API.

Stream:
A Stream is a logical connection between the Java application and the file through
which we can send the data to the file and read the data from file.

 We can send data between Java application and file in two forms.
1. Binary form
2. Character form
 Based on the form(type) of data flowing between Java program and file, streams are
divided into 2 types.
1. Binary Streams
2. Character Streams
 Based on the direction of the data flow between the Java program and the file, Binary
stream is divided into 2 types.
1. Input Stream
2. Output Stream
 Similarly, Based on the direction of the data flow between the Java program and the
file, Character stream is divided into 2 types.
1. Reader
2. Writer

Q ) Why InputStream, OutputStream, Reader and Writer have different sub-classes? Why not
to implement all the methods related to reading and writing are implemented in one class?
The data can be read from and written into different sources and destinations. From
sources Java application reads data, and it writes data to destinations.
Below are the commonly available sources and destinations:
The sources can be a Keyboard, Mouse, File, Database, Socket (Computer), object,
Array, String, StringBuffer.
The destinations can be a Monitor, File, Database, Socket (Computer), object, Array, String,
StringBuffer.
Reading data is different from source to source and also writing data is different from
destination to destination. So, the logic required to read and write data must be implemented
specific to each source and destination separately. That is why the logic required to read and
write data specific to each source is implemented in a separate class.
Note: We follow a naming convention to choose the name for a subclass. The name of the
sub-class should consists of the operation name and the name of the super class.
Sub-class name = operation name + Super class name
For example, the name of the super class is EventListener. If the operation name is action,
then we choose the name of the sub-class as ActionListener.
In IO Streams,
Sub-class name = source or destination name + InputStream/Reader
or OutputStream/Writer.
Ex: FileInputStream, FileOutputStream, FileReader, FileWriter,
SocketOutputStream, etc.,
OutputStream, InputStream, Reader and Writer classes are abstract classes.
Because read() and write() methods are declared as abstract methods in these classes.
Remaining method are concrete methods.
Why read() and write() methods are declared as abstract methods?
Reading data is different from source to source and also writing data is different from
destination to destination. So, the logic required to read and write data must be implemented
specific to each source and destination separately. Hence, these methods are declared as
abstract methods. So that subclasses of InputStream and OutputStream classes will definitely
implement these two methods.
Note:
 If we want to declare all operations of my object to be implemented by the sub-classes,
we should choose the interface as our super class.
 If we want to implement some of the operations and some of the operations are
declared to be implemented by the sub-classes, we should choose Abstract class as our
super class.
 If we want to implement all the operations and we want to just reuse our operations
by the sub-classes, we should go for Concrete class.
InputStream and OutputStream class hierarchy:
For each type of source and destination in java.io package sun has given a separate class.
Below diagram shows InputStream and OutputStream classes' hierarchy.
InputStream class hierarchy:

You might also like