程式扎記: [ Data Structures with Java ] Section 23.2 : Binary Files

標籤

2011年4月21日 星期四

[ Data Structures with Java ] Section 23.2 : Binary Files


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 :
  1. FileInputStream fin = new FileInputStream("dataIn.dat");  
  2. FileOutputStream fout = new FileOutputStream("dataOut.dat");  
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 :
- Demonstration code :
  1. public static void Prog23_1(){  
  2.     int intVal = 100;  
  3.     short shortVal = 1500;  
  4.     long longVal = 429496729L;  
  5.     byte[] buf = {35271510012755};  
  6.       
  7.     try{  
  8.         DataOutputStream fout = new DataOutputStream( new FileOutputStream("data.dat"));  
  9.         fout.writeInt(intVal);  
  10.         fout.writeShort(shortVal);  
  11.         fout.writeLong(longVal);  
  12.         fout.write(buf, 0, buf.length);  
  13.         fout.close();  
  14.           
  15.         DataInputStream fin = new DataInputStream(new FileInputStream("data.dat"));  
  16.         System.out.println("int:\t"+fin.readInt());  
  17.         System.out.println("short:\t"+fin.readShort());  
  18.         System.out.println("long:\t"+fin.readLong());  
  19.         byte[] b = new byte[fin.available()];  
  20.         System.out.println("byte array:");  
  21.         fin.read(b);  
  22.         for(int i=0; i
  23.             System.out.print(b[i]+" ");  
  24.         }  
  25.         System.out.println();  
  26.         fin.close();  
  27.     } catch(FileNotFoundException fnfe) {  
  28.         System.err.println("Cannot create \"data.dat\" ("+fnfe.getMessage()+")");  
  29.     } catch(IOException ioe) {  
  30.         System.err.println("Error: "+ioe.getMessage());  
  31.     }  
  32. }  

Output :
int: 100
short: 1500
long: 429496729
byte array:
3 5 2 7 15 100 127 55
This message was edited 3 times. Last update was at 06/11/2010 15:46:11

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!