aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B 5 Chapter 6 Question 12 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B 5 Chapter 6 Question 12" Watch "K&B 5 Chapter 6 Question 12" New topic
Author

K&B 5 Chapter 6 Question 12

richard rehl
Ranch Hand

Joined: May 21, 2007
Posts: 36
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?
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
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).


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
richard rehl
Ranch Hand

Joined: May 21, 2007
Posts: 36
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.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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.


SCJP 1.6 96%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B 5 Chapter 6 Question 12