Data streams read and write strings, integers, floating-point numbers, and other data that is commonly presented at a higher level than mere bytes. The
java.io.DataInputStream
and
java.io.DataOutputStream
classes read and write the primitive Java data types (boolean, int, double, etc.) and strings in a particular, well-defined, platform-independent format. Since DataInputStream and DataOutputStream use the same formats, they are complementary. What a data output stream writes, a data input stream can read. These classes are especially useful when you need to move data between platforms that may use different native formats for integers or floating-point numbers.
- Data Stream Classes
Chaining Streams in Java I/O Classic
Every filter stream has an underlying stream that supplies the actual bytes of data. You specify the underlying stream that supplies the data in the filter stream's constructor. To create a new
DataOutputStream connected to a
FileOutputStream you might do this:
FileOutputStream fos = new FileOutputStream("ln.txt");
DataOutputStream lnos = new DataOutputStream(fos);
It is not uncommon to combine these into one line like this:
DataOutputStream lnos = new DataOutputStream(new FileOutputStream("ln.txt"));
Modern Java
Chain multiple Filters in Sequence
You can chain multiple filters in sequence as shown in the following code:
FileInputStream fis = new FileInputStream("data.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
Now the data input stream
dis performs buffered reads from the file data.txt.