简体   繁体   English

java.nio.file.Paths - 如何在迭代时跳过无法访问的文件/文件夹

[英]java.nio.file.Paths - How to skip unaccessible file/folder while iterating

I am writing code to iterate through all folders that i can access.我正在编写代码来遍历我可以访问的所有文件夹。 However my program stops when on folder is inaccessible.但是,当文件夹无法访问时,我的程序会停止。

How do i bypass that folder and keep looking for others?我如何绕过该文件夹并继续寻找其他文件夹? Or, in other words, how do i show folders and files i have access to?或者,换句话说,我如何显示我有权访问的文件夹和文件?

i am using java.nio.file.Paths (Java 8).我正在使用 java.nio.file.Paths (Java 8)。

Here's my code:这是我的代码:

import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Paths;

// Test File D:\TEST\test.jpg
public class TestIteration{

    // Main
    public static void main(String[] args) throws Exception {
        // Iterate through folders
        String rootFolder = "c:\\";    
        WalkDirTree(rootFolder); // Walks through directory tree and print names of directories/files.      
    }


    // Prints all file/directory names in the entire directory tree
    private static void WalkDirTree(String rootFolder) throws Exception {                   
        Files.walk(Paths.get(rootFolder)).forEach(path -> {
            System.out.println(path);
        });

        System.out.println("_______________________________________DONE");
    }

}

If by inaccessible you're referring to "not readable", then I reckon you can use Files#isReadable ;如果无法访问您指的是“不可读”,那么我认为您可以使用Files#isReadable from its documentation:从它的文档:

Tests whether a file is readable.测试文件是否可读。 This method checks that a file exists and that this Java virtual machine has appropriate privileges that would allow it open the file for reading.此方法检查文件是否存在以及此 Java 虚拟机是否具有允许它打开文件进行读取的适当权限。 Depending on the implementation, this method may require to read file permissions, access control lists, or other file attributes in order to check the effective access to the file.根据实现,此方法可能需要读取文件权限、访问控制列表或其他文件属性,以检查对文件的有效访问。 Consequently, this method may not be atomic with respect to other file system operations.因此,该方法对于其他文件系统操作可能不是原子的。

Your code would look like:您的代码如下所示:

Files.walk(Paths.get(rootFolder)).filter(Files::isReadable).forEach(path -> {
    System.out.println(path);
});

While streams are certainly beautiful, you might need to deal with each inaccessible file individually.虽然流当然很漂亮,但您可能需要单独处理每个无法访问的文件。

In that case, you could choose to have a simple for loop by using the FileUtils.listFiles method from org.apache.commons.io在这种情况下,您可以选择使用org.apache.commons.ioFileUtils.listFiles方法来创建一个简单的for循环

String[] extensions = {"png","txt"}; // file extensions you might want to target
Collection fileObjects= FileUtils.listFiles(new File("dirPath"),extensions,true);
for (Object obj : fileObjects){
     File file = (File) obj;
     if (!Files.isReadable(file.toPath())) continue; // or do smth else
}

If you wish to target all files regardless their extensions, then pass null instead of the a String array.如果您希望针对所有文件而不考虑其扩展名,则传递null而不是String数组。

If you do not wish recursive search, then pass false instead of true如果您不希望递归搜索,则传递false而不是true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 nashorn javascript 中使用 Paths(java.nio.file.Paths) - How to use Paths(java.nio.file.Paths) in nashorn javascript Java.nio.file.Paths为当前目录提供了错误的路径? - Java.nio.file.Paths is giving incorrect path for current directory? Java6中java.nio.file.Paths的替代方法 - Alternative to java.nio.file.Paths in Java6 使用java.nio.file.Paths接口时缺少方案(IllegalArgumentException) - Missing scheme (IllegalArgumentException) while using java.nio.file.Paths interface JMeter - 在类'java.nio.file.Paths'中找不到静态方法get(java.lang.String) - JMeter - Static method get( java.lang.String ) not found in class'java.nio.file.Paths' 使用 java.nio.file.Paths & jsfml loadFromFile 时发生死锁 - Deadlock occurring when using java.nio.file.Paths & jsfml loadFromFile 复制文件时,JMeter Bean Shell采样器错误“在类'java.nio.file.Paths中找不到静态方法get(java.lang.String)” - JMeter Bean Shell Sampler error “…Static method get( java.lang.String ) not found in class'java.nio.file.Paths” when copying files 爪哇蔚来| 路径和文件 | 如何使用从其他对象获取的自定义名称创建自定义文件和文件夹? - Java NIO | Paths & Files | How to create custom file and folder with custom name fetched from other object? 无法解析以下内容:java / nio / file / Paths; - Failed resolution of: java/nio/file/Paths; 非静态等效于 java.nio.file.Paths.get() - Non-static equivalent to java.nio.file.Paths.get()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM