English 中文(简体)
阅读多案
原标题:Read Multiple Files In Java

我想一劳永逸地读到多个档案。 姓名如下:

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

有1 000多份档案,我不想用类似于以下的法典,在 Java一处书写所有档案:

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

或许应当按相反顺序阅读档案名称。 我如何能够这样做?

问题回答

将所有档案放在一个文件夹中(文件夹在档案清单中):

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

a. 将所有档案放在一个文件夹中,但不包括分文件夹:

    // 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();
        }
    }));

3. 将档案清单改为对案件敏感的指令:

    // 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());
        }
    });

You can use listFiles method to obtain all the files in the folder.

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());
} 

我认为这是解决你问题的办法。

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 String array inside the directory.

在采取这一步骤之后,你可以按照你想要的方式对档案进行分类(例如,用名称、括号、名称)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签