PREV NEXT
- A simple java program takes an input, process the input and produces the output as result.
- Input can be taken from the keyboard, from a file, from main memory or from elsewhere. Now if the input is defined inside the program itself, then running it anytime would present us the same output.
- In Order for an effective use of the program the input has to be received from outside of the program and hence IO functions are defined.
Streams:
- Streams are used to handle input/output operations.
- Stream is like a pipe which carries data from one end to the other. The end points are nothing but the source and destination of the data to be read/written.
Getting input from console and writing in a file.
Streams are classified into two types, which are the parent class of all the subclasses of other streams.
- InputStream – Superclass of all classes used to read the input.
- OutputStream – Superclass of all classes used to write the output.
There are 3 fields in System class which represents some stream.
- in – Represents the standard input device
- out – Represents the standard output device
- err – same as System.out (Errors are displayed using this field.)
- System.in field represents the InputStream object. Both System.out and System.err fields represents the PrintStream object.
- To accept the data from the input device, we need to connect the System.in (representing the basic input device) to an input stream.
Example:
| InputStreamReader object = new InputStreamReader(System.in); |
Based on how we are going to read/write the data, we have 2 categories in streams.
- Byte Streams – ASCII 8 bits (each character requires 1 byte)
- Character Streams – Unicode 16 bits (each character requires 2 byte)
ByteStreams:
- Byte streams are used to read/write input/output of 8 bit bytes.All kind of data (text,image,audio,video) can be read/written using byte streams.
The following diagrams shows some of the subclasses of InputStream and Output Streams which falls under the category of byte streams.
