I am using the code below, but it’s not working. The method canWrite() is not working, even if changing the rights for writing for a directory.
File file = new File(fc.getSelectedFile().getAbsolutePath());
// fc is a FileChooser object
if(f.canWrite())
{
// write access
}
else
{
// no write access
}
I have also tried:
try
{
AccessController.checkPermission(new FilePermission("/tmp/*", "read,write"));
System.out.println("Good");
// Has permission
}
catch (SecurityException e)
{
// Does not have permission
System.out.println("Bad");
}
The
File.canWrite()method is behaving as its specification says it should:(Emphasis added).
It is returning
falsebecause the object is not a file.If you are using Java 7 (or later), one solution would be to use
Files.getAttributeto retrieve the relevant attribute(s) to determine the access. Note that the attribute you use may be operating system specific. (I’m sure Google could find examples for you.)