Google+

Like Us On Facebook


Total Pageviews

264. I/O Streams







I/O Streams (Input/Output Streams) in Java are used to do any of the below things -

  • Reading information into Program using Input Stream
  • Writing information from Program using Output Stream

Example for an Input Stream -

Suppose if we want to read test data from an excel document and use the test data in our Java programs, we have to use Input Stream Classes which can read the text from files.

Example for an Output Stream -

Suppose if you want to write your program output to a text file on your computer, you have to use the Output Stream Classes which can write your program output to the files.

Diagrammatic representation of Input and Output Streams -

A program uses input stream to read data from a data sources like files etc -


A program uses output stream to write data to a data source like files etc -



Types of I/O Streams -

There are two types of I/O Streams in Java -
  • Byte Streams
  • Character Streams
Byte Streams - Classes under this stream are used for handling input and output data in bytes.

Character Streams -  Classes under this stream are used for handling input and output in characters.


I/O Class Hierarchy -

Please find the below diagram to understand the hierarchy of  I/O Classes (Dont remember all the Classes in the below hierarchy as we may not be using all the classes. This is just for your reference)  -


Understanding the hierarchy -
  • InputStream and OutputStream are the parent classes of all the classes that handle input and output data in byte. (Represented in above diagram in Orange)
  • Reader and Writer are the parent classes of all the classes that handle input and output data in characters. (Represented in above diagram in Blue)

We have these many Classes for handling the Input and Output starting from primitive data types to advanced level objects. But I am going to work on the I/O Classes which only handling Input and Output with Files. The following are the I/O Classes which handle Input Output using files as data source -
  • FileReader - Handles Input by characters
  • FileWriter - Handles Output by characters
  • FileInputStream - Handles Input by bytes
  • FileOutputStream - Handles Output by bytes
FileReader is used to read the character stream in the file i.e. a file that contains only the character means FileReader is the choice to read the file. On the other hand if the file contains image, byte like raw data format means FileInputStream is the choice to read the data in the file.

As we may only read a file containing text during Automation, I am going to explain the below two of the above four Classes first -
  • FileReader - Handles Input by characters
  • FileWriter - Handles Output by characters
Hence we have understood the concept of I/O Streams. Lets implement the above two I/O Classes in upcoming posts first following by the remaining two.






Please comment below to feedback or ask questions.

Reading text file using read( ) method of FileReader I/O Class will be explained in the next post.












No comments:

Blog Index