Checking for write access in a directory before creating files inside it

后端 未结 4 2049
长情又很酷
长情又很酷 2020-12-16 09:49

My small utility application asks the user for an output directory via a GUI file selector. Then it creates a lot of files in this output directory after some processing.

相关标签:
4条回答
  • 2020-12-16 10:45

    you can use FilePermission to get the details . I find one way where you need to implement SecurityManager the code is here and here

    0 讨论(0)
  • 2020-12-16 10:49

    it doesn't works even if you invoke canWrite on the final path?

    File sample = new File(fileBrowse.getParent(),"empty.txt"); 
    
    if (sample.canWrite()) {
        doSomethingUseful(sample);
    } else {
        notifyUser();
    }
    
    0 讨论(0)
  • 2020-12-16 10:49

    Using Java 1.8 I was able to use the following.

    Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(Paths.get(destDir), LinkOption.NOFOLLOW_LINKS);
    Assert.assertTrue("User did not have read permission.", permissions.contains(PosixFilePermission.OWNER_READ));
    Assert.assertTrue("User did not have execute permission.", permissions.contains(PosixFilePermission.OWNER_EXECUTE));
    Assert.assertTrue("User did not have write permission.", permissions.contains(PosixFilePermission.OWNER_WRITE));
    
    Assert.assertFalse("Group did have read permission.", permissions.contains(PosixFilePermission.GROUP_READ));
    Assert.assertFalse("Group did have execute permission.", permissions.contains(PosixFilePermission.GROUP_EXECUTE));
    Assert.assertFalse("Group did have write permission.", permissions.contains(PosixFilePermission.GROUP_WRITE));
    
    Assert.assertFalse("Others did have read permission.", permissions.contains(PosixFilePermission.OTHERS_READ));
    Assert.assertFalse("Others did have execute permission.", permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
    Assert.assertFalse("Others did have write permission.", permissions.contains(PosixFilePermission.OTHERS_WRITE));
    
    0 讨论(0)
  • 2020-12-16 10:52

    You could check the file permissions, make sure the directory exists, and do a lot of checking or find a library that does all that checking for you BUT (!) isn't the best way of checking to try ? If you check for permissions and the filesystem changes... you will have to change your code. But trying to write a file will ALWAYS tell you if you can write a file.

    Your solution doesn't have to be the most elegant one. It's not a cheap hard coded patch or something ugly. It's just normal code. And it will always work. But if you don't like to see that check in the code just separate it by putting it in class which only goal is to check for the possibly of writing. In fact, you should put it in a utility class wheter you like the elegance or not.

    The other solution would be to place your whole writing-to-the-hard-drive code, in the try. And if you can't write, the whole part will be skipped and you give feedback to the user with a message in the catch part.

    0 讨论(0)
提交回复
热议问题