Data OutputStream and Datalnput Stream

FileOutputStream and FileInputStream are useful for writing and reading bytes and arrays of bytes. However, they provide no support for writing and reading primitive type values (such as integers) and strings.

For this reason, Java provides the concrete DataOutputStream and DataInputStream filter stream classes. Each class overcomes this limitation by providing methods to write or read primitive type values and strings in a platform-independent way:

■ Integer values are written and read in big-endian format (the most significant byte comes first). Check out Wikipedia's "Endianness" entry (http://en.wikipedia.org/wiki/Endianness)to learn about the concept of endianness.

■ Floating-point and double precision floating-point values are written and read according to the IEEE 754 standard, which specifies four bytes per floating-point value and eight bytes per double precision floating-point value.

■ Strings are written and read according to a modified version of UTF-8, a variable-length encoding standard for efficiently storing two-byte Unicode characters. Check out Wikipedia's "UTF-8" entry (http://en.wikipedia.org/wiki/Utf-8) to learn more about UTF-8.

DataOutputStream declares a single DataOutputStream(OutputStream out) constructor. Because this class implements the DataOutput interface, DataOutputStream also provides access to the same-named write methods as provided by RandomAccessFile.

DataInputStream declares a single DataInputStream(InputStream in) constructor. Because this class implements the DataInput interface, DataInputStream also provides access to the same-named read methods as provided by RandomAccessFile.

Listing 10-18 presents the source code to a DataStreamsDemo application that uses a DataOutputStream instance to write multibyte values to a FileOutputStream instance, and uses DataInputStream to read multibyte values from a FileInputStream instance.

Listing 10-18. Outputting and then inputting a stream of multibyte values import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

public class DataStreamsDemo {

final static String FILENAME = "values.dat";

public static void main(String[] args) {

DataOutputStream dos = null; DataInputStream dis = null;

FileOutputStream fos = new FileOutputStream(FILENAME); dos = new DataOutputStream(fos); dos.writeInt(1995);

dos.writeUTF("Saving this String in modified UTF-8 format!"); dos.writeFloat(1.0F);

dos.close(); // Close underlying file output stream.

// The following null assignment prevents another close attempt on // dos (which is now closed) should IOException be thrown from // subsequent method calls. dos = null;

FileInputStream fis = new FileInputStream(FILENAME);

dis = new DatalnputStream(fis);

System.out.println(dis.readInt());

System.out.println(dis.readUTF());

System.out.println(dis.readFloat());

dis.close(); // Close underlying file input stream.

catch (IOException ioe) {

System.err.println(ioe.getMessage()); try if (dos != null) dos.close();

catch (IOException ioe2) // Cannot redeclare local variable ioe. System.err.println(ioe2.getMessage());

catch (IOException ioe2) // Cannot redeclare local variable ioe. System.err.println(ioe2.getMessage());

DataStreamsDemo creates a file named values.dat, calls DataOutputStream methods to write an integer, a string, and a floating-point value to this file, and calls DataInputStream methods to read back these values. Unsurprisingly, it generates the following output:

1995

Saving this String in modified UTF-8 format! 1.0

CAUTION: When reading a file of values written by a sequence of DataOutputStream method calls, make sure to use the same method-call sequence. Otherwise, you are bound to end up with erroneous data and, in the case of the readUTF() methods, thrown instances of the UTFDataFormatException class (a subclass of IOException).

Was this article helpful?

0 0

Post a comment