File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A questin in Marcus's tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A questin in Marcus Watch "A questin in Marcus New topic

A questin in Marcus's tutorial

Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
Here is a question in Marcus Green's tutorial;
Which of the following statements are true?
1) The RandomAccessFile class allows you to move forwards and backwards without re-opening the file
2) An instance of RandomAccessFile may be used as a constructor for FileInputStream
3) The methods of RandomAccessFile do not throw exceptions
4) Creating a RandomAccessFile instance with a constructor willl throw an exception if the file does not exist.
The answer is 1 and 4.
I think answer 4 is wrong because according to the API, the constructor for RandomAccessFile will create a file if it doesn't exist. The FileNotFoundException will only be thrown if the file exists but is a directory rather than a regular file, or cannot be opened or created for any other reason.
My problem is I'm having trouble trying to cause a FileNotFoundException with the following code.(Of course most of the times, it is the other way around )

I have already created a directory called "dir" under the current directory. But there is no exception. Maybe I'm wrong with the string representation of a directory. Can someone tell me how to make the computer know "dir" is a directory and generate an exception?

Sun Certified Java Programmer
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
If you create a RandomAccessFile with "rw" mode, then it will create the file and not throw an exception. If you create a RandomAccessFile with "r" mode and the file doesn't exist it will throw an error.
If the file is a directory, it will throw a FileNotFound Access denied error for either mode.
Why you are not seeing the error is becuase you are catching the error but not printing any output from it. In your catch block, have a System.out.println(ioe); and you will see the errors.
I agree. Here's the link:
subject: A questin in Marcus's tutorial
It's not a secret anymore!