Hi there Lawrence I'm only just learning Java, but here is what I reckon...
Question 1 ---------- -1 will never be returned, so the readInt() method attempts to read past the end of the file after it has read in the value of 455. This makes sense, or it would imply that we wouldn't be able to read and write integer values of -1 without creating a file terminator :D When the IOException is commented out and the code executed, the EOFException is being passed to the next level up in the hierarchy - in this case to the main() method. This is why the main() method is referenced in the stack trace. When the IOException is uncommented and the code is executed, it appears that there are no errors. The reason for this is because the IOException catch block doesn't do anything with the exception. If you put a System.out.println("Oops - we have a problem here"); inside the IOException block, you will see that it gets executed every time. EOFException inherits from IOException and is handling the EOFException in this case. You can prove that be changing the IOException to EOFException and re-running the code. The results will be the same. I tried a simple code change to test this out and changed your code to: import java.io.*; public class RandomFileAccessHandling { /** * @param args */ public static void main(String[] args) throws Exception{ try { RandomAccessFile raf = new RandomAccessFile("/home/ocd/ Training/Java/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 for (int i = 0; i < 4; i++) { System.out.println(raf.readInt()); } raf.close(); } catch (EOFException e) { System.out.println("Oops - we have a problem here..."); } } } ...and this runs without any errors. (Note: my filename is for a Linux box) Question 2 ---------- Have you checked out db4o for this? db4o allows you to read and write objects quite easily using an Object-Oriented Database. You can download db4o for free (non-commercial use) and it comes with a really cool tutorial. Here is a link: http://www.db4o.com/DownloadNow.aspx I hope this helps. Wayne Riesterer :) On Feb 18, 4:34 am, Lawrence Louie <lawrence.lo...@gmail.com> wrote: > 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 javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en