• Post Reply Bookmark Topic Watch Topic
  • New Topic

How different between File.exists() and File.canRead() ???  RSS feed

 
Karn Sadyapongse
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my program must check that
if the path that user input is correctly or not
(path on the server-side)

i have to check that the path is correct or not
which method is for this case ? File.canRead() or File.exists() ??

thanks
 
Tom Sullivan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.File;
From the API:

"public boolean exists()Tests whether the file or directory denoted by this abstract pathname exists.

"public boolean canRead()Tests whether the application can read the file denoted by this abstract pathname.

One tests whether or not the file is there and the other tests whether or not it can be read (for instance that the file does not have a lock on it and can actually be read)...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can imagine many situations where File.exists() returns true, but File.canRead() will return false. Tom gives one example. Another example is if the file is on a Unix-like system with permissions set to 600 (i.e. only the owner can read and write to the file), then File.canRead() will return false if you are not the owner.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!