如何使用FileNameFilter列出目录中的最新文件 [英] How to list latest files in a directory using FileNameFilter

查看:62
本文介绍了如何使用FileNameFilter列出目录中的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含xml文件的目录:

I have a directory which contains xml Files :

 20140110-093721565_TOTO.xml
 20140110-093821565_TATA.xml
 20140110-094021565_TOTO.xml
 20140110-091021565_TOTO.xml
 20140110-093921565_TATA.xml
 ....
 20140110-091021565_TETE.xml

文件名的第一部分表示文件生成的时间:20140110-09102156

The first part of the file name represents the time the file was generated : 20140110-09102156

 20140110 = Date
 09102156 = Hour

最后一部分用于标识文件:TOTO

and the last part is used to identify the file : TOTO

我想要列出的是每个文件的最新版本,例如:

What I want is to list the last version of each files like :

20140110-091021565_TOTO.xml
20140110-093821565_TATA.xml

如何定义FileNameFilter?

How can I define a FileNameFilter ?

推荐答案

我不确定是否有一种方法可以提供过滤器来将结果限制为一个,因为文件名会不断变化.这是一种实现您想要的东西的方法.

I'm not sure there is a way to provide filter that will restrict the result to just one in your case because the file names are ever variying. Here is one way to acheive what you wanted.

File f = new File("C:\\");  // what ever directory you want. 

FilenameFilter totoFilter = new FilenameFilter()
{
    public boolean accept(File dir, String name) 
    {
        String lowercaseName = name.toLowerCase();
        if (lowercaseName.endsWith("TOTO.xml")) return true;
        else                                    return false;

    }
};

FilenameFilter tataFilter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        String lowercaseName = name.toLowerCase();
        if (lowercaseName.endsWith("TOTO.xml")) return true;
        else                                    return false;
    };

    String[] tataFiles = f.list(tataFilter);
    Arrays.sort(tataFiles, Collections.reverseOrder());

    String[] totoFiles = f.list(totoFilter);
    Arrays.sort(totoFiles, Collections.reverseOrder());

    System.out.println(tataFiles[0]);
    System.out.println(totoFiles[0]);
}

我写了另一种逻辑,即使您必须添加类型,它也可以帮助您用更少的新行来扩展想要实现的目标.这遵循了我在评论中提到的方法.您所要做的就是向数组列表添加定义另一种文件类型的新序列.

I have written an alternative logic that can help you extend what you want to achieve with less number of new lines even when you have to add types. This follows the approach I mentioned in the comment. All you have to do is add new sequence that defines another type of file to array list.

    File f = new File("C:\\"); // current directory

    ArrayList<String> sequence = new ArrayList<>();
    sequence.add("TATA");
    sequence.add("TOTO");

    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            String lowercaseName = name.toLowerCase();
            if (lowercaseName.endsWith(".xml"))
                return true;
            else
                return false;
        }
    };

    String[] totoFiles = f.list(filter);
    Arrays.sort(totoFiles, Collections.reverseOrder());
    for (String type : sequence) {
        int i = 0;
        for (; i < totoFiles.length; i++) {
            if (totoFiles[i].contains(type))
                break;
        }
        System.out.println(totoFiles[i]);
    }

这篇关于如何使用FileNameFilter列出目录中的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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