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がない場合は常に例外をスローするようです。をキャッチすることでこれを処理できます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 に答える