Find files from a directory in Java

F

In Java, it’s not so hard to find the specific files you need from a directory. You can easily do that with the code below:

final String directory = \\path\to\directory\;
final File file = new File(directory);
File[] matchingFiles = file.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return !name.startsWith("myfile") && !name.endsWith("txt");
    }
});

The first file =

matchingFiles[0];

The second file =

matchingFiles[1];

and so on

Add comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tag Cloud

Categories

%d bloggers like this: