|
public static void main(String[] args) throws IOException
{
DataFile myFile;
myFile = new DataFile("datafile" ); // Open the file named "datafile"
/* ==============================
Read and print 4 integers
============================== */
int k;
while ( true ) // Read all integer in file
{
k = myFile.ReadInt();
System.out.println("Number read: " + k);
}
}
|
This "solution" will fail:
cs377@aruba (505)> java readAll1 Number read: 1090535936 Number read: 1124073472 Number read: 68 Number read: 0 Exception in thread "main" java.io.EOFException at java.io.RandomAccessFile.readInt(RandomAccessFile.java:803) at DataFile.ReadInt(DataFile.java:71) at readAll1.main(readAll1.java:23) |
because:
|
cd /home/cs377001/demo/Phys-Data-Dependance/file0 java readAll1 |
public static void main(String[] args) throws IOException { DataFile myFile; myFile = new DataFile("datafile" ); // Open the file named "datafile" /* ============================== Read and print 4 integers ============================== */ int k; while ( true ) { try { k = myFile.ReadInt(); System.out.println("Number read: " + k); } catch( java.io.IOException e ) { System.out.println("Program entered in CATCH section"); System.out.println("Done reading the file !!!"); break; // exit the while loop !!! } } } |
cd /home/cs377001/demo/Phys-Data-Dependance/file0 java readAll2 |