Author Topic:   RandomAccessFile Class
jvarkha
greenhorn
posted April 27, 2000 02:58 PM         
Hi,
Going through Applied Reasoning mock exam, I came across an answer saying that "When an instance of RandomAccessFile class is created with the read/write mode, the underlying file will be created if it does not already exist." Its given as true, but I'm not sure about it. Any more info regarding this would be appreciated.

Thanks.

maha anna
bartender
posted April 27, 2000 03:06 PM             
jvarkha,
Please take it in the right sense. I am saying for your good only. The best way to test anything in one time and only one time and no more re-thinking is Just write a few lines of code and see the result.

The result and the concept just stays in your mind and it is you who created the source and latter you can be confident of what you say when you explain the same to others.

I hope you don't tell Maha I didn't ask you . Come back if you have anything more to clarify.

regds
maha anna

jvarkha
greenhorn
posted April 27, 2000 03:13 PM         
import java.io.*;

public class RAF
{
public static void main(String args[])throws IOException
{
long len =0;
try
{
RandomAccessFile myRAF = new RandomAccessFile("RAF.txt","rw");
System.out.println("Length of file is :" + myRAF.length());
}
catch (IOException ioe)
{
System.out.println("IOExceotion occurred.");
}
}
}


/* Missed you again by a few minutes Maha Anna. Well, that was what I was doing, and after the compilation and successful execution, a RAF.txt was created. So I guess it is true. Bit its contrary to what RHE states. Am I correct ? */

Thanks.

maha anna
bartender
posted April 27, 2000 04:38 PM             
jvarkha
Yes. You are right. When a RandomAccessFile object is created with the mode "rw" it creates the file if it does't exist already provided the given path is valid. R&H is WRONG in their book. I guess you didn't correct the book with their errata page.

If you haven't you have a lots of correction to do.
The online R&H Errata linke is here.

regds
maha anna

[This message has been edited by maha anna (edited April 27, 2000).]

jvarkha
greenhorn
posted April 27, 2000 04:51 PM         
Thanks a lot Maha, looks like I do have lots to check in their errata and be clear. Thanks for the link too.

maha anna
bartender
posted April 29, 2000 08:19 AM             
Jvarkha,
I checked in R&H errata page. This error is STILL NOT reported there. (Page 383/The paragraph under the sample code/"No file is created on the file system" ). This is just an information.

regds
maha

satya5
ranch hand
posted April 29, 2000 11:28 AM             

Just an observation ...

The main() in the above code uses the throws construct
to specify the IOException and the method also catches it.
Though redundant, java compiler doesnot complain.

I din't know that ....

Regds.

- satya

maha anna
bartender
posted April 29, 2000 12:28 PM             
Satya,
The compiler doen't complain when a method says in its declaration that it throws any type of Throwable. It is because, logically thinking, the method is declaring that it throws the Throwable. The code at present inside the method may not throw the Throwable. What if someone adds a code in future which might throw any of Throwable ? . It is like that.

Mainly this concept is used where as a designer you just design the skeleton of the overall s/w. (i.e) you are desigining

1. What are all the packages
2. what classes go under which package
3. what methods are inside which class
4. What Exceptions you expect the methods to throw

So the point which you noticed here comes into picture in the 4th point I stated above. You can just define all abstract methods and say, Fello collegues! These are the exceptions I expect these methods to throw. So while implementing the methods you take care of that. like that.

regds
maha anna

[This message has been edited by maha anna (edited April 29, 2000).]

|