[英]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.io
的FileUtils.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.