在 Java 中读取多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7499130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:15:02  来源:igfitidea点击:

Read Multiple Files In Java

javafilenamesreadfile

提问by user951487

I want to read multiple files into Java at once. File names are like:

我想一次将多个文件读入 Java。文件名类似于:

  • nnnnn_UM2012.txt
  • ghkjdf_UM2045.txt
  • erey_UM2189.txt
  • ....
  • nnnnn_UM2012.txt
  • ghkjdf_UM2045.txt
  • erey_UM2189.txt
  • ....

There are over 1,000 files and I do not want to write all files names in Java one by one, using code similar to the following one:

有1000多个文件,我不想把所有文件名用Java一一写出来,使用类似于以下的代码:

String fileNames = {"nnnnn_UM2012.txt","ghkjdf_UM2045.txt","erey_UM2189.txt", …}

Maybe the filenames should be read in reverse order. How can I do that?

也许应该以相反的顺序读取文件名。我怎样才能做到这一点?

回答by Mike Kwan

You can use listFilesmethod to obtain all the files in the folder.

您可以使用listFiles方法获取文件夹中的所有文件。

回答by Steve McLeod

To get all files in a folder (sub-folders are included in the list of files):

要获取文件夹中的所有文件(子文件夹包含在文件列表中):

    // get all files in the folder
    final File folder = new File(".");
    final List<File> fileList = Arrays.asList(folder.listFiles());

To get all files in a folder, excluding sub-folders:

要获取文件夹中的所有文件,不包括子文件夹:

    // get all files in the folder excluding sub-folders
    final File folder = new File(".");
    final List<File> fileList = Arrays.asList(folder.listFiles(new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    }));

To sort the list of files into reverse case-sensitive order:

要将文件列表排序为区分大小写的反向顺序:

    // sort the files into reverse order
    Collections.sort(fileList, new Comparator<File>() {
        public int compare(File o1, File o2) {
            return o2.getName().compareTo(o1.getName());
        }
    });

To sort the list of files into reverse case-insensitive order:

要将文件列表排序为不区分大小写的反向顺序:

    // sort the files into reverse order ignoring case
    Collections.sort(fileList, new Comparator<File>() {
        public int compare(File o1, File o2) {
            return o2.getName().compareToIgnoreCase(o1.getName());
        }
    });

回答by Adithya

You can follow the below approach if all the files are in a single directory. Get a reference to the directory by providing its fully qualified path and then use the list()function get all the file names into a Stringarray inside the directory.

如果所有文件都在一个目录中,您可以遵循以下方法。通过提供目录的完全限定路径来获取对目录的引用,然后使用list()函数将所有文件名放入String目录内的数组中。

After this step you can sort the files according to the way you want (For e.g. by its name,length,etc..).

在这一步之后,您可以根据您想要的方式(例如,按名称、长度等)对文件进行排序。

回答by Manuel Leduc

File rep = new File("path to rep");
File[] list = rep.listFiles();
ArrayList<String> filenames = new ArrayList<String>();
for ( int i = 0; i < list.length; i++) {
    filenames.add(list[i].getName());
} 

I guess it can be a solution to your problem.

我想它可以解决您的问题。