[英]What is the most effective way to read names of a lot of files?
I need to get names of a lot of folders and files in the folders.我需要获取文件夹中许多文件夹和文件的名称。 I tried using FileUtils
, but it's too slow compared to java's native Files.walk()
, and I can't use it because of that reason.我尝试使用FileUtils
,但与 Java 的本机Files.walk()
相比它太慢了,因此我无法使用它。
But I can't even use the java's Files.walk()
because when it tries to read a hidden file, it throws Exception
and stops (the hidden file cannot be avoided, someone said it's a design mistake - Files.walk skip directories ).但我什至不能使用 java 的Files.walk()
因为当它试图读取一个隐藏文件时,它抛出Exception
并停止(隐藏文件无法避免,有人说这是一个设计错误 - Files.walk 跳过目录) . Is there any different way?有什么不同的方法吗? I don't know what to do now.我现在不知道该怎么办。
Kindly do the following in your code.-请在您的代码中执行以下操作。-
import org.apache.commons.io.filefilter.HiddenFileFilter;
I will give you an example-我给你举个例子——
package test;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import org.apache.commons.io.filefilter.HiddenFileFilter;
public class ListHiddenAndVisibleFilesTest {
public static void main(String[] args) throws IOException {
File directory = new File(".");
System.out.println("Hidden Files:");
File[] hiddenFiles = directory.listFiles((FileFilter) HiddenFileFilter.HIDDEN);
for (File hiddenFile : hiddenFiles) {
System.out.println("hidden file: " + hiddenFile.getCanonicalPath());
}
System.out.println("\nVisible Files:");
File[] visibleFiles = directory.listFiles((FileFilter) HiddenFileFilter.VISIBLE);
for (File visibleFile : visibleFiles) {
System.out.println("visible file: " + visibleFile.getCanonicalPath());
}
}
}
This gives result like这给出了类似的结果
Hidden Files:
hidden file: C:\kum\workspace\test\hidden1.txt
hidden file: C:\kum\workspace\test\hidden2.txt
Visible Files:
visible file: C:\kum\workspace\test\.classpath
visible file: C:\kum\workspace\test\.project
visible file: C:\kum\workspace\test\visibleFile.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.