Java Streams  «Prev  Next»

Lesson 2Reading data from a stream
ObjectiveWrite program that reads data from System.in.

Reading Data from Java Streams

Write a program that reads data from System.in using an InputStream and prints the numeric value of each byte read. The fundamental method of the InputStream class is read().

Read 10 bytes from System.in

The following code fragment reads 10 bytes from the System.in input stream and stores them in the int array b:

int[] b = new int[10];
for (int i = 0; i < b.length; i++) {
  b[i] = System.in.read();
}

Notice that although read() is reading a byte, it returns an int. If you want to store the raw bytes instead, you can cast the int to a byte

If read() encounters the end-of-stream, it returns -1 instead.
You use this as a end of stream flag to watch for the end-of-stream.
reading data stream exception
The read() method is declared to throw an IOException. The java.io.IOException is a checked exception, so you will need to wrap all calls to this method in a try-catch block or declare that your own method throws IOException.

The read() method waits or blocks until a byte of data is available and ready to be read. Input and output can be slow, so if your program is doing anything else of importance, you should try to put I/O in its own thread.
Although read() is declared abstract, all the concrete subclasses of InputStream you will actually be dealing with do implement it. Because InputStream is abstract, you can never instantiate it directly, only its subclasses.

Reading Data - Exercise

Click the Exercise link below to write a program that reads data from System.in using an InputStream and prints the numeric value of each byte read using the System.out.println() method.
Reading Data - Exercise

Java I/O

Character Streams and Byte Streams

Consider the difference between Java source files and class files generated by the compiler. The Java source files have extension of .java and are meant to be read by humans as well as programming tools such as compilers. However, the Java class files have extension of .class and are not meant to be read by humans but are meant to be processed by low-level tools such as a JVM (executable java.exe in Windows) and Java disassember (executable javap.exe in Windows).
  1. We refer to human-readable files containing text (or characters) as text files;
  2. we refer to the machine readable or low-level data storage files as binary files.

Naturally, how you interpret what is inside text files vs. binary files is different. For example, in text files, you can interpret the data read from the file and differentiate between a tab character, whitespace character, and newline character. However, you do not deal with data from binary files like that; they are low-level values. To give another example, consider a .txt file you create with a text editor such as Notepad in Windows; it contains human-readable text. Now, consider storing your photo in a .bmp or .jpeg file; these files are certainly not human readable. They are meant for processing by photo editing or image manipulation software, and the files contain data in some pre-determined low-level format.

The java.io package has classes that support both character streams and byte streams. You can use character streams for text-based I/O. Byte streams are used for data-based I/O. Character streams for reading and writing are called readers and writers, respectively (represented by the abstract classes of Reader and Writer). Byte streams for reading and writing are called input streams and output streams, respectively (represented by the abstract classes of InputStream and OutputStream). Table 3-2 summarizes the differences between character streams and byte streams for your quick reference.

Differences Between Character Streams and Byte Streams
Table 3-2. Differences Between Character Streams and Byte Streams