Java Input/Output  «Prev   Next»

Lesson 4Data streams
ObjectiveData streams use Java-defined data types

Data Input Streams

DataInputStreams and DataOutputStreams have additional methods for reading data types. That is, instead of reading and writing bytes, they can read and write the data types defined by Java.
For example, these classes have methods for reading and writing integers, floating-point numbers, characters, and boolean values.
Instances of both DataInputStream and DataOutputStream are created based on an underlying stream. The idea is that these streams are piped. An instance of DataInputStream, for example, is tied to an instance of class InputStream. The InputStream subclass reads the individual bytes; DataInputStream assembles them into Java's data types.
Figure 07.61
Figure 07.61

DataInputStream

When you create a DataInputStream, you tie it to some other subclass of class InputStream.
Here is aDataInputStream example.
It's also useful to know about two subclasses of InputStream and OutputStream that are associated with files. These are called FileInputStream and FileOutputStream. FileInputStream and FileOutputStream instances are created by specifying a file to associate with these streams. Once you create a FileInputStream instance, reading from this instance reads from the file. And of course, writing to an instance of class FileOutputStream writes to the file.

Exercise: Read using DataInputStream

Here is the second part of the exercise concerning reading from the standard input.
This exercise asks you to modify the program you wrote in the previous exercise to read from DataInputStream.
Read Using Buffered Reader - Exercise

Java DataStreams - Quiz

When you finish, there is a quiz to review the material presented.
Java DataStreams - Quiz