在Java7中的FileWalking树中解决访问被拒绝的问题 [英] Working around access denied in a FileWalking Tree in Java7

查看:162
本文介绍了在Java7中的FileWalking树中解决访问被拒绝的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些简单的代码,仅用于测试 Files.walkFileTree()方法。但是,文件夹 / etc / ssl / private ,具有这些权限( rwx - x --- )抛出一个异常,即使我以为我用if语句来保护它( if(permissions.equals(rwx - x ---))。

The following is some simple code just to test the Files.walkFileTree() method. However, the folder /etc/ssl/private, which has these permissions (rwx--x---), throws an exception, even when I thought I guarded it with an if statement (if (permissions.equals("rwx--x---")).

我做错了什么?先谢谢。

What am I doing wrong? Thanks in advance.

public static void main (String []args) throws IOException, InterruptedException
{       
    Files.walkFileTree(Paths.get("/"), new WalkingTheThing2());
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
{
    PosixFileAttributeView posixView = Files.getFileAttributeView(dir, PosixFileAttributeView.class);
    PosixFileAttributes posixAttr = posixView.readAttributes();
    String permissions =PosixFilePermissions.toString(posixAttr.permissions());
    if (permissions.equals("rwx--x---"))
    {
        return FileVisitResult.SKIP_SUBTREE;
    }

    return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
        throws IOException {
    try{

        System.out.println(file.getFileName()+" " +Files.size(file));

        return FileVisitResult.CONTINUE;
    }
    catch(IOException io){return FileVisitResult.CONTINUE;}
}

我得到的例外是: java.nio.file.AccessDeniedException:/ etc / ssl / private

编辑:重写 visitFileFailed

public FileVisitResult visitFileFailed(Path file, IOException io)
{   
    return FileVisitResult.SKIP_SUBTREE;
}


推荐答案

虽然覆盖 visitFileFailed 解决了你的问题,它可能隐藏了你仍然做错了几件事:

Although overriding visitFileFailed solved your problem, it might be hiding the fact you are still doing several things wrong:


  1. Files.getFileAttributeView 可以返回null(例如,如果文件系统不支持POSIX文件权限),使 posixView.readAttributes() NPE失败

  2. posixView.readAttributes()本身可以抛出异常(例如,如果您没有所需的读取权限)文件的权限) - 这可能是 AccessDeniedException 你得到的原因

  3. 并非完全错误,但比较字符串权限的表示可能不适合此用例,除非您要显式检查文件是否具有给定的权限 - 并且具有其他权限;另一种方法是检查所需的权限:

  1. Files.getFileAttributeView can return null (e.g. if the file system does not support POSIX file permissions) making posixView.readAttributes() fail on NPE
  2. posixView.readAttributes() can itself throw exceptions (e.g. if you don't have the required permissions to read the file's permissions) - this might be the cause of the AccessDeniedException you got
  3. Not entirely wrong, but comparing the string representation of the permissions might be inappropriate for this use case, unless you want to explicitly check that the file has the given permissions - and does not have the the others; a different approach would be to check just for the required permissions:

Set<PosixFilePermission> perm = posixAttr.permissions();
if (perm.contains(OWNER_READ) || perm.contains(GROUP_READ) || perm.contains(OTHERS_READ)) {
    return FileVisitResult.SKIP_SUBTREE;
}


这篇关于在Java7中的FileWalking树中解决访问被拒绝的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆