Java Input and Output
Contents
Java Input and Output :
- Java I/O is the process to produce the output based on the input given
- Most of the Java I/O Streams classes are in java.io package.
- Most of the Java File I/O classes are in the java.nio.file package.
Java Stream Concept :
Consider the example of the water pipeline, Water flows in the pipeline then we can say that its stream of water. Similarly when the sequence of the data flows from input device to output device and vice versa then we can say that its a stream of data.
- I/O Stream represents input source or an output destination.
- Different kinds of sources and destinations are - disk files, devices, other programs, and memory arrays.
- Different kinds of data are - byte,data types, characters, objects
Three I/O Streams :
| Stream | Purpose |
|---|---|
| System.out | standard output stream |
| System.in | standard input stream |
| System.err | standard error |
Java Output Stream :
- Java application uses an output stream to write data to a destination
- Output Stream may be a file , an array , peripheral device or socket.

OutputStream class
- OutputStream class is an abstract class.
- OutputStream is superclass of all classes representing an output stream of bytes.
- An output stream accepts output bytes and sends them to pool
| Method | Description |
|---|---|
| public void write(int)throws IOException | Used to write a byte to the current output stream. |
| public void write(byte[])throws IOException | Used to write an array of byte to the current output stream. |
| public void flush()throws IOException | Used to flush the current output stream. |
| public void close()throws IOException | Used to close the current output stream. |
Java Input Stream :
- Java application uses an input stream to read data from source
- Input Stream may be a file, disk, object or peripheral device or socket.

InputStream class
- InputStream class is an abstract class.
- InputStream is the superclass of all classes representing an input stream of bytes.
Commonly used methods of InputStream class
| Method | Description |
|---|---|
| public abstract int read() throws IOException | reads the next byte of data from the input stream.It returns -1 at the end of file. |
| public int available() throws IOException | returns an estimate of the number of bytes that can be read from the current input stream. |
| public void close() throws IOException | is used to close the current input stream. |
