import java.io.*;
public class RandomFileAccessHandling {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//try {
RandomAccessFile raf = new RandomAccessFile("E:\\temp\\abc\
\test.txt", "rw");
raf.writeInt(10);
raf.writeInt(43);
raf.writeInt(88);
raf.writeInt(455);
// change the 3rd integer from 88 to 99
raf.seek((3 - 1) * 4);
raf.writeInt(99);
raf.seek(0); // go to the first integer
int i = raf.readInt();
while (i != -1) {
System.out.println(i);
i = raf.readInt();
}
raf.close();
//} catch (IOException e) {
//}
}
}
Question 1:
If I comment out the IOException, the output is the following:
10
43
99
455
Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:739)
at
com.ibm.test.JavaIO.RandomFileAccessHandling.main(RandomFileAccessHandling.java:
27)
In the main method, I already have the throws Exception. Why would I
require to have a catch IOException there still?
Question 2:
If I want to do a random access that's is not using primitive or its
wrapper class, say Student object, how can I use the read or write
method using the class? How can I determine determine the number of
bytes so that I would need where to do the seek? Thx.
Lawrence
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en