Java IO Streams are notorious for being complex.
Part of the cause for this is because Java is a platform-independent language and a majority of Java applications are not console based. As a result, the JVM has to be able to communicate with many different kinds of input and output devices.
The JVM uses Java IO streams to accomplish this.
I think of a stream as a flow of information, either flowing into the program or flowing away from it.
An INPUT stream flows INTO a program, bringing information into the application.
An OUTPUT stream flows OUT OF a program, carrying information out of the application.
There are two basic types of I/O streams – those composed of characters(16-bit) and those composed of bytes(8-bit). I visualize one as sort of an alphabet soup of characters, and the other as a Matrix-style flow of binary 0’s and 1’s.
This gives up four basic types of streams:
- Character stream as input – Reader (Alphabet soup flowing in)
- Character stream as output – Writer (Alphabet soup flowing out)
- Byte stream as input – InputStream (Binary soup flowing in)
- Byte stream as output – OutputStream (Binary soup flowing out)
The chart below shows these four basic types of streams, which are all abstract classes for Java Input and Output streams.
Remember that abstract classes cannot be instantiated! This means you cannot create an object of Reader, Writer, InputStream, or OutputStream class.
They are SUFFIXES for their child classes.
Sometimes a program needs byte stream input (or output) converted into character stream input (or output) or vice versa. For this, we have the bridge classes, OutputStreamWriter and InputStreamReader
There are also some predefined streams in Java, which we need to know about:
- System.in – the standard input stream from the keyboard
- System.out – the standard output stream to the console
- System.err – the standard error stream, which usually goes to the console (but can be redirected to log errors, etc.)
Please note that these are BYTE streams!!!
So, after all this theory, here’s how to get input from a user typing:
First of all, we know that Java has a predefined byte stream from the keyboard, called…
Because it’s a byte stream, we need to wrap it in a bridge class to convert it to a character stream…
InputStreamReader isr = new InputStreamReader(System.in);
Then, we need to wrap THAT stream in a buffer so that the individual characters get strung together like beads to form a string:
BufferedReader br = new BufferedReader(isr);
Now we have a buffered character stream called br. We can use methods from this stream’s class to read information provided by the user. For instance,
String myString = br.readLine();
This declares a String object called myString, which we initialize at the next line of text that the user inputs. We retrieve that line of text by calling a method from our buffered character stream.
Remember that you need to parse the user’s input if you need an int, double, or other data type.
To parse an integer, use:
int myInt = Integer.parseInt(br.readLine())
To parse a double, use:
double myDouble = Double.parseDouble(br.readLine())
For further information, there’s always the Oracle documentation!