Thanks Mihai - your comments make it quite a bit clearer. For some reason, they weren't visible when I posted my last entry.
Regards - Wayne Riesterer On Feb 20, 4:46 pm, Mihai DINCA <mihai.di...@free.fr> wrote: > Hi Lawrence > > This is a very nice explanation from Lawrance. > > Concerning just the double management of the exceptions: > -- The main method throws any Exception > -- But catches the IOException > > It means that IOExceptions alone are processed in the "catch" block: "do > nothing" in the original code. So that no error message is displayed. > The "exceptional" behavior of the code (reaching the end of the file for > instance) is accepted by the programmer as long as it concernes only the > I/O. > > Throwing Exception means that the exception is not processed here, it is > notified to the caller in order to be processed at a higher instance. As > there is no higher instance for the "main" method except for the JVM > itself, the JVM processes the exception by displaying the error message > and the call stack. > > You may see the exceptions as alerts that stop the current execution up > to a block able to process and consume it. The "try/catch" structure is > able to consume an exception, so it becomes invisible outside. A method > block containing the "throw" clause propagates the corresponding > exception up to the next higher block able to process and to consume it. > > In the case you comment the try/catch structure, the end of file > generated IOException is propagated outside the "main" method, together > with any other java.lang.Exception that may occur (as the IOException is > a subclass for Exception). > > If you leave the try/catch structure in place, then the IOException is > "consumed" at the try/catch structure, so it doesn't reach the method > block level to be propagated outside. > > Of course, if you add the "throws Exception" to the "main" method, the > compiler no longer force you to use a try/catch. This is a syntactical > point of view. But just what the syntax allows is not necessarily what > you want to do. In this case, you may want not to write a big alert > about reaching the end of the file, as you consider it as an expected > behavior. > > Concerning the reads on random objects from a file: your program must > know in advance what is in the file in order to do be able to read its > content or to skip the necessary number of bytes up to a particular > object. Of course, this cannot be always accomplish, for example, your > database file may contain a mix of objects, according to whatever the > end user stored in it. You cannot handle this without using much more > sophisticated techniques. > > Databases use specific tricks to cheat. One solution is to have fixed > length records and to store in a file only records of the same kind. For > example, if your database is supposed to store Authors and Books, then > they have to be stored in two different files. > > Another solution is to have fixed length blocks of bytes containing one > or more variable length objects. Each time an object is written, the > whole block is altered and the nature of the object, its actual length > and its position in the block is written in the block header. When you > read, you read full blocks and you retrieve the particular object you > need based on the information in the header. > > In both cases, your file is simply a file of bytes and it is up to you > to assemble bytes into objects and to split object properties into bytes. > > Of course, the easier was is to use some already existent database, as > sugested by Lawrance. > > Hope it helps > Mihai > > Le 20/02/2011 08:10, Lawrence Louie a �crit : > > > > > > > > > Hi Wayne, > > > Thanks for the reply/ > > > For Question #1: > > One thing that is not clear here is Exception is a superclass of all > > other exception, which means that this should catch the exception if > > any. So my understanding is that it should not require an exception > > of EOFException inside the main. Do you agree? > > > For Question #2: > > What this example deals with here is the random file access? This > > means from my understanding is that we would need to understand the > > data inside the object in order to figure out how to move the data > > around. However, if the data object is a wrapper class which is a > > user defined data objects and not using any database object, it > > appears it is not easy to figure out the data access method using the > > RandomAccessFile method. Any suggestion about this? > > > Thanks! > > > Lawrence > > On Sat, Feb 19, 2011 at 5:52 PM, Wayne Riesterer > > <nexus...@westnet.com.au <mailto:nexus...@westnet.com.au>> wrote: > > > 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 > > <mailto: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 > > <mailto:javaprogrammingwithpassion@googlegroups.com> > > To unsubscribe from this group, send email to > > javaprogrammingwithpassion+unsubscr...@googlegroups.com > > <mailto:javaprogrammingwithpassion%2bunsubscr...@googlegroups.com> > > For more options, visit this group at > > http://groups.google.com/group/javaprogrammingwithpassion?hl=en > > > -- > > To post to this group, send > > ... > > read more » -- 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