Java Reference
In-Depth Information
Display 10.22
Random Access to a File (part 2 of 2)
40 System.out.println("Returning to location 3,");
41 System.out.println("where we wrote the double.");
42 ioStream.seek(3);
43 double oneDouble = ioStream.readDouble();
44 System.out.println("The double value at location 3 is "
45 + oneDouble);
The location of readDouble
must be a location where
writeDouble wrote to the file.
46 ioStream.close();
47 }
48 catch (FileNotFoundException e)
49 {
50 System.out.println("Problem opening file.");
51 }
52 catch (IOException e)
53 {
54 System.out.println("Problems with file I/O.");
55 }
56 System.out.println("End of program.");
57 }
58 }
Sample Dialogue
The dialog assumes the file bytedata
did not exist before the program was run.
Writing 3 bytes to the file bytedata.
The length of the file is now = 3
The file pointer location is 3
Moving the file pointer to location 1.
The value at location 1 is 2
The value at the next location is 3
Now we move the file pointer back to
location 1, and change the byte.
The value at location 1 is now 9
Now we go to the end of the file
and write a double.
The length of the file is now = 11
Returning to location 3,
where we wrote the double.
The double value at location 3 is 41.99
End of program.
Byte locations are numbered
starting with zero.
Three 1-byte values and 1 double
value that uses 8 bytes =
11 bytes total.
 
Search WWH ::




Custom Search