Java I/O is an API which is used to read and write data (Input and Output) and it is located in package called java.io.The Java IO package is primarily focused on input and output to files, network streams, internal memory buffers etc. or in sample terms we can say it perform file handling in java.
Java I/O API is mostly concerned for reading the raw data from from a source and writing the raw data to a destination. What could be these source and destination?
- Network connection
- System.in, System.out, System.error
- In memory-Buffers (e.g. arrays)
A stream is conceptually endless flow of data. It could be in byte form or character form. We can either read from a Stream and write to a Stream
Program needs InputStream or Reader to read the data from source and requires OutputStream or writer to write the data to some destination.
In java, we mostly see 3 kinds of stream as mentioned below.
- System.in: standard input stream (read data from console)
- System.out: standard output stream (Write data to console)
- System.err: standard error stream (Write data to console)
These streams are attached with the console. Let’s understand these with examples.
Read input from console.
int i = System.in.read();
Write output and error to Console.