Preface :
Recall that a text file contains characters with a newline sequence separating lines. A binary file consists of data objects that vary from a single byte to more complex structures that include integers, floating-point values, programmer-generated class objects, and arrays. In this section, we give an overview of binary file I/O and use it in the implementation of Huffman compression in Section 23.3
DataInput and DataOutput Streams :
In Chapter 2, we introduce the Java stream hierarchy from text input and output. The design of the hierarchy begins with abstract Reader and Writer classes have objects that are attached to a physical file on disk as the underlying character stream. Most applications use a BufferedReader and PrintWriter stream from text I/O. A BufferedReader serves as a filter on the underlying input stream and provides the method readLine() to extract a line of characters. A PrintWriter object has versions of print() and println() that prints formated representations of primitive data and objects to a text-output stream.
Java handles I/O for binary streams in a similar way. The abstract classes InputStream and OutputStream define basic byte stream operations. FileInputStream and FileOutputStream are concrete classes that extend the respective abstract classes. Their objects are attached to physical files on disk that provide an underlying byte stream. For instance :
Upper declarations are binary input and output streams that are attached to the files dataIn.dat and dataOut.dat respectively. These file streams process only a single byte or an array of bytes. Often, an application needs formated I/O to read and write primitive data types. For binary files, the filter streams DataInputStream and DataOutputStream provides these operations. A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.
These classes, DataInputStream and DataOutputStream, have a number of methods that cover all of the primitive type along with array and string handling. We need only a few of the methods for our implementation of Huffman compression.
- Program 23.1
This program simply illustrates methods in the DataInputStream and DataOutputStream classes. After writing an int value, a short value and a long value to the binary file "data.out" using a DataOutputStream, the program writes an array of bytes to the file. After closing the file, the program opens a DataInputStream and reads the int, short and long values and outputs them. At this point, only the byte array remains unread. Using the method available()to determine the number of bytes remaining in the file, the application inputs the array and output its values :
Output :
Recall that a text file contains characters with a newline sequence separating lines. A binary file consists of data objects that vary from a single byte to more complex structures that include integers, floating-point values, programmer-generated class objects, and arrays. In this section, we give an overview of binary file I/O and use it in the implementation of Huffman compression in Section 23.3
DataInput and DataOutput Streams :
In Chapter 2, we introduce the Java stream hierarchy from text input and output. The design of the hierarchy begins with abstract Reader and Writer classes have objects that are attached to a physical file on disk as the underlying character stream. Most applications use a BufferedReader and PrintWriter stream from text I/O. A BufferedReader serves as a filter on the underlying input stream and provides the method readLine() to extract a line of characters. A PrintWriter object has versions of print() and println() that prints formated representations of primitive data and objects to a text-output stream.
Java handles I/O for binary streams in a similar way. The abstract classes InputStream and OutputStream define basic byte stream operations. FileInputStream and FileOutputStream are concrete classes that extend the respective abstract classes. Their objects are attached to physical files on disk that provide an underlying byte stream. For instance :
- FileInputStream fin = new FileInputStream("dataIn.dat");
- FileOutputStream fout = new FileOutputStream("dataOut.dat");
These classes, DataInputStream and DataOutputStream, have a number of methods that cover all of the primitive type along with array and string handling. We need only a few of the methods for our implementation of Huffman compression.
- Program 23.1
This program simply illustrates methods in the DataInputStream and DataOutputStream classes. After writing an int value, a short value and a long value to the binary file "data.out" using a DataOutputStream, the program writes an array of bytes to the file. After closing the file, the program opens a DataInputStream and reads the int, short and long values and outputs them. At this point, only the byte array remains unread. Using the method available()to determine the number of bytes remaining in the file, the application inputs the array and output its values :
Output :
This message was edited 3 times. Last update was at 06/11/2010 15:46:11
沒有留言:
張貼留言