We help IT Professionals succeed at work.

Java File properties canRead, canWrite, canExecute

finaris
finaris asked
on
Hi Experts,

for checking file attributes read/write/execute, I currently use
java.io.File.canRead()
java.io.File.canWrite()
java.io.File.canExecute()

In some cases, these functions do NOT return expected results on Windows:

1) canWrite() is TRUE for files on a CD/DVD drive
2) canWrite() is TRUE for files that i cannot access due to missing privileges
3) canWrite() is FALSE for files that do not exist
...

Is a better API/library available to solve all this, or will I have to implement native OS-specific code for this issue?

I already tried the Apache VFS library, which does not return expected results either.
Comment
Watch Question

Commented:
Which version of Java are you using?  
Version 1.7 gave us new API for files. I haven't used them. You could test  these static methods.  They are in the  java.nio.file  package.
Files.isExecutable(path)
Files.isReadable(path)
Files.isWritable(path)
zzynxSr. Software engineer

Commented:
I support rrz's proposal.

Remark:
>> 3) canWrite() is FALSE for files that do not exist
Seems correct to me. (You cannot write to an unexisting file)

Documentation about canWrite():
true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.

Author

Commented:
works as expected, thanks