How to check write permissions of a directory in java?

后端 未结 8 1755
粉色の甜心
粉色の甜心 2020-12-29 01:13

I would like a code snippet that checks whether a directory has read/write permissions and do something if it does, and does something else if it doesnt. I tried an example

相关标签:
8条回答
  • 2020-12-29 02:04

    if you just want to check if you can write:

    File f = new File("path");
    if(f.canWrite()) {
      // write access
    } else {
      // no write access
    }
    

    for checking read access, there is a function canRead()

    0 讨论(0)
  • 2020-12-29 02:09

    You should use the path of the directory alone ("/tmp") to query the permissions of a directory:

    AccessController.checkPermission(new FilePermission("/tmp", "read,write"));
    

    With "/tmp/*" you query the permissions of all files inside the /tmp directory.

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