升序读取多个文件文件名 [英] Read Multiple Files in ascending order File Name

查看:67
本文介绍了升序读取多个文件文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道发生了什么事.我想访问其中包含多个文件的目录:

I dont know what is happening to me. I want to access a directory with multiple files in it suppose:

 folder\\1.txt 2.txt 3.txt....

现在我想根据它们的出现来阅读它们,我的意思是首先降到最低,只是我想按升序阅读它们! 我的代码是:

now I want to read them according to their occurrence I mean first comes the lowest simply I want to read them in ascending order! My code is:

File f=new File("xxx");
File[] files = f.listFiles(); 
for (File ff : files) {

   if(ff.isFile()) {
   System.out.println(ff.toString());
   }
}

到目前为止,代码可以正常工作,但是将文件作为

so far the code works fine but it takes the files as

1.txt
10.txt
11.txt
9.txt
8.txt
...

出什么问题了,我想按升序顺序阅读它们

so what is going wrong i want to read them orderly in ascending order

推荐答案

只需按 Arrays.sort 进行排序:

        Arrays.sort(files, new Comparator<File>() {

            public int compare(File o1, File o2) {
                int n1 = getNum(o1.getName());
                int n2 = getNum(o2.getName());
                return n1 - n2;
            }
       }

        private int getNum(String name) {
            int i;
            // extract number from the file name here by doing some processes
            return i;
        }

这篇关于升序读取多个文件文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆