• 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

K&B 5 Chapter 6 Question 12

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
and that the invocation is issued from a directory that has two subdirectories, "dir1" and "dir2", and that "dir1" has a file "file1.txt" and "dir2" has a file "file2.txt".

Question: why is the output "false true"? The first time through the for loop, path = "dir1", and file = ("dir1", "file2.txt"), but file2.txt doesn't exist in that directory. So why isn't a FileNotFoundException generated instead?

Update: never mind, just looked in the API and see that it

Returns:
true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise
Throws:
SecurityException -


So I guess my question is aimed more at the Java developers - wouldn't it make more sense if this method threw a FileNotFoundException if a file wasn't found? Hm?
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do some operation on that file object then you will get FileNotFoundException.

For Example: Add this line in your code:


When you run your code after adding above line, you will get FileNotFoundException at 1st time and 2nd time you don't get..

File.isFile method checks if it is file returns true else false (in case if that is a directory or file doesn't exist).

Note: File object just represent the file, it doesn't check whether it exists or not physically.( This is my assumption, I may wrong).

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

richard rehl wrote:So I guess my question is aimed more at the Java developers - wouldn't it make more sense if this method threw a FileNotFoundException if a file wasn't found? Hm?



Personally, I would find it really annoying if it did that. Exceptions are for exception conditions. Reporting that the file doesn't exist, when the task is to check if the file exist, is hardly an exception condition.

Henry
 
richard rehl
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Personally, I would find it really annoying if it did that. Exceptions are for exception conditions. Reporting that the file doesn't exist, when the task is to check if the file exist, is hardly an exception condition.

I guess the reason I ask is that there already is a method to do just that, File.exists(). File.isFile(), on the other hand, would seem to be a method that assumes that a file exists and checks to see if it is actually a file. In which case, if the file really wasn't there, it would make sense to throw an exception. Seems logical to me.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
file.exist() check for both a file and a dir whatever is represented by the file object, whereas file.isfile() is checking only for file.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic