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?
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. Bill