3

您如何检查应用程序是否有权读取或写入文件系统上的特定目录。我正在尝试这个:

try {
    AccessController.checkPermission(new FilePermission(files[i]
            .getAbsolutePath(), "read,write"));
}
catch (Exception e) {
    System.out.println(e.toString());
}

但它总是抛出异常并输出:

java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Dell" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Documents and Settings" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\glut-3.7.6" "read")

我在这里缺少什么吗?

4

2 回答 2

1

AccessController当您没有权限时,似乎总是抛出异常。你可以通过 catch 来处理这个问题SecurityException

java.io.File.canRead()使用,方法检查读/写权限的更好解决方案java.io.File.canWrite()

于 2012-07-31T11:50:42.353 回答
0

文档中的文本:

 Determines whether the access request indicated by the
 specified permission should be allowed or denied, based on
 the security policy currently in effect, and the context in
 this object. The request is allowed only if every ProtectionDomain
 in the context implies the permission. Otherwise the request is
 denied.
于 2021-04-21T08:28:19.067 回答