Page 654 - Introduction to Programming with Java: A Problem Solving Approach
P. 654

                620 Chapter 15 Files char readChar()
int readInt()
double readDouble()
For example, suppose you had declared an array of double values called doubleData. In a try block after the above file-opening statement, you could fill this array with data from the binary file using code like this:
for (int i=0; i<doubleData.length; i++)
{
}
doubleData[i] = fileIn.readDouble();
Common Properties
The FileOutputStream and FileInputStream constructors throw FileNotFoundExceptions, and the methods in the DataOutputStream and DataInputStream classes throw IOExceptions. So you can use an IOException catch block to catch them all. Of course, you should close each file when you’re finished with it, and you can use close methods inherited by the DataOutputStream and DataInputStream classes to perform this operation. It’s easiest to include the close statement with the opening and transfer statements in the same try block.
The DataInputStream class does not include a viable line-input method. Although such a method
does exist,9 it’s often better to read through '\n' characters and accumulate text input in larger chunks than
lines. To do this, use the two-byte null character whose code value is zero to terminate a string-reading
fileOut.writeChar(0);
Then in your file-reading program, you can check each character as it comes in to see whether its code value equals zero. When it does, you’re at the end of the string.
Structured Binary Files
Binary file I/O is easiest when file data is all one type. With some work, however, you can mix data types and put structure into a binary file. Doing this gives you a sense of what Java’s built-in code does in the next section’s object I/O. So let’s look at a simple example of a structured binary file. Suppose you have a binary file that contains some entries from a database table. To read this information, you need to know how that file is structured. Let’s say you know it has this relatively simple organization:
1. The first data block is for a text title and/or description. It is composed of an unspecified number of 2-byte char’s. This text block is terminated by the null character, whose code value is a sequence of two zero bytes.
2. Theseconddatablockisasingle4-byteint.Itgivesthenumberofsub-blocksinthethirdblock.
3. The third block contains a number of identical sub-blocks. Each of these sub-blocks has two fields:
a) The first field is a 4-byte int.
b) The second field is an 8-byte double.
In your file-reading program, suppose you want to put the two values in each sub-block into the two
instance variables of a new object of type Record. And suppose Record has a two-parameter constructor that initializes these two variables.
9 If your binary file contains only text, instead of a DataInputStream, it’s better to use a BufferedReader, and the BufferedReader class includes a readLine method.
operation. You can append this character to your string when writing to a binary file with this statement:
Apago PDF Enhancer
 







































































   652   653   654   655   656