Basics of Input/Output Programming

Input/ Output operations are not easy in java as they are in c or c++. Java language is mostly used to develop web application or desktop application. In both cases user interface is there to take input from user or displaying information to user. For example in web application HTML can be used to create forms which will take input from user or in desktop application text box can be used to take input from user. Very few console application developed in java.

In java input/output for files, i/o devices and network socket are same. If you understand the concepts of input/output in java then you can write program to deal with input/output with files, socket etc.


In java input/output performed with the use of streams. Streams represent connection between our program and the file/ socket/ devices. Streams is connection throught which information flows from program to destination or from source to program. This scenario is same with accessing data from database. first we have to make a connection with database and through this connection data is transferred.

In java to perform input/output, we have to make connection also known as stream. Streams represent continuous flow of information. Streams are unidirectional, which means one stream can be used to transfer data in one direction. It's same as water pipe which can be used to take water in or out.

Stream depicted in above diagram is from program to destination. Destination could be file, socket or device. This stream is used to transfer information from program to destination, Also known as output stream. Data is going out from program.
Whenever there is need to write information to file, socket or device, Output stream is used. In order to perform reading and writing both on same file, two streams required one for reading and one for writing.

Stream which used in reading from file, allow flow of information from file to program. It means information is coming from file to program. This stream is known as input stream.

Type of files

In earlier version of java, there was only byte streams are availabe to deal with binary files. Binary files are not human readable files. In jdk 1.2 character streams were also added. Why we need to have two different streams.

    1. Binary File

    Binary files can't be read. Reason for using binary file is saving of disk space. For example in binary file if we store 42 then it would take only one byte.
    To handle binary files we need stream which allow flow of information in the form of binary. These stream known as byte stream.

    2. Character Files

    These files store information in human readable format. Content of these files can readable directlly.
    To deal with character stream we need stream which allow flow of information in the form of characters. These stream known as character stream.

In brief we can say in java we are two options byte stream, character stream. For reading input stream and for writing operation output stream required.