• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

A questin in Marcus's tutorial

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic