Question

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

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

 20140110 = Date
 09102156 = Hour

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

How can I define a FileNameFilter ?

Was it helpful?

Solution

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

OTHER TIPS

I don't quite understand what you want to do but DirectoryStream<Path> can be used to retrieve specific files in a directory. You obtain DirectoryStream<> using the static method of nio.file.Files class and optionally, you may pass in a glob or a filter to choose what you need / do not need.

Quoting the docs for DirectoryStream.Filter:

An interface that is implemented by objects that decide if a directory entry should be accepted or filtered. A Filter is passed as the parameter to the Files.newDirectoryStream(Path,DirectoryStream.Filter) method when opening a directory to iterate over the entries in the directory.

To sum it up, write a custom filter that implements the DirectoryStream.Filter interface and put your search criteria logic in accept()

I am not sure It is what you need, but i think you can retrieve data and time from file name, parse it to Date time and get the biggest one, so it will be the last version, right?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top