用Java读取多个文件

发布于 2024-12-06 06:20:05 字数 332 浏览 1 评论 0原文

我想一次将多个文件读入Java。文件名如下:

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

有超过1,000个文件,我不想用Java一一写下所有文件名,使用类似于以下代码的代码:

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

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

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

  • 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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想挽留 2024-12-13 06:20:05

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

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

获取文件夹中的所有文件(不包括子文件夹):

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

将文件列表按大小写相反的顺序排序:

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

对列表进行排序将文件转换为不区分大小写的反向顺序:

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

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());
        }
    });
夏夜暖风 2024-12-13 06:20:05

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

单调的奢华 2024-12-13 06:20:05
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());
} 

我想这可以解决你的问题。

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.

杀お生予夺 2024-12-13 06:20:05

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文