简体   繁体   English

读取大量文件名的最有效方法是什么?

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

相关问题 从TCP套接字读取的最有效方法 - Most effective way to read from a TCP socket 坚持HashMap的最有效方法是什么? - what is the most effective way to persist HashMap? 使用Java 7在生产应用程序中没有任何泄漏的情况下读取文件的最有效方法是什么 - What is the most effective way to read a file without any leaks in production app using java 7 读取/写入在线applet的大.ser文件的最有效方法 - Most effective way to read/write large .ser file for online applet 在 Jsp 中打印字符串数组的 Arraylist 最有效的方法是什么? - What is the most effective way of printing Arraylist of string array in Jsp? 最小化通过UDP传输数据的最有效方法是什么? - What is the most effective way to minimize data for transfer over UDP? 在GCS上解压缩gzip文件的最有效方法是什么? - What is the most effective way to make unzipped copy of a gzipped file on GCS? 从int值创建BigInteger实例的最有效方法是什么? - What is the most effective way to create BigInteger instance from int value? 重构这个简单方法的最有效方法是什么? - What's the most effective way to refactor this simple method? 从服务器加载谷歌地图标记的最有效方法是什么 - What is the most effective way to load google maps markers from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM