如何在java中测试文件权限?

dffbzjpn  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(194)

我正在写一个文件浏览器。实际上效果还不错。但是当我试图打开特定的文件时,我得到了一个错误。当我在windows的文件资源管理器中键入这些文件的路径时,我没有访问它们的权限,所以我猜这就是错误的原因。不,我不想在我的文件浏览器中显示那些文件。
我测试了以下内容:

File F = new File(path);
    Path P = Paths.get(path);
    if(Files.isWritable(P)==true){
        //do stuff
    }

File F = new File(path);
    Path P = Paths.get(path);
    if(Files.isExecutable(P)==true){
        //do stuff
    }

而不是 isWriteable 我也试过:

F.canWrite();

F.canRead();

不幸的是,它总是返回true,即使我无法访问文件。同样让我困惑的是,错误如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at inst.FilesChange.ShowFiles(FilesChange.java:29)
at inst.FilesChange.NextFiles(FilesChange.java:54)
at inst.JFD3Installer$5.mouseClicked(JFD3Installer.java:127)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)

但是文件列表:

File L[] = F.listFiles();

不是空的什么的,但我可以在错误

L.lenght();

这也不起作用:

if(L.length>0){
     /do stuff
  }

谢谢你的帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题