Using File.listFiles with FileNameExtensionFilter
Asked Answered
D

7

94

I would like to get a list of files with a specific extension in a directory. In the API (Java 6), I see a method File.listFiles(FileFilter) which would do this.

Since I need a specific extension, I created a FileNameExtensionFilter. However I get a compilation error when I use listFiles with this. I assumed that since FileNameExtensionFilter implements FileFilter, I should be able to do this. Code follows:

FileNameExtensionFilter filter = new FileNameExtensionFilter("text only","txt");
String dir  = "/users/blah/dirname";
File f[] = (new File(dir)).listFiles(filter);

The last line shows a compilation error:

method listFiles(FileNameFilter) in type File is not applicable for arguments of type FileNameExtensionFilter

I am trying to use listFiles(FileFilter), not listFiles(FileNameFilter). Why does the compiler not recognize this?

This works if I write my own extension filter extending FileFilter. I would rather use FileNameExtensionFilter than write my own. What am I doing wrong?

Dishonest answered 22/4, 2011 at 0:56 Comment(2)
First off, you need to have "txt" as the 1st argument and "text only" as second.Kayceekaye
The code is correct, @lobster1234. See: docs.oracle.com/javase/7/docs/api/javax/swing/filechooser/…Lubbi
E
197

The FileNameExtensionFilter class is intended for Swing to be used in a JFileChooser.

Try using a FilenameFilter instead. For example:

File dir = new File("/users/blah/dirname");
File[] files = dir.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.toLowerCase().endsWith(".txt");
    }
});
Exciter answered 22/4, 2011 at 1:1 Comment(0)
Q
55

One-liner in java 8 syntax:

pdfTestDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".txt"));
Queenstown answered 26/2, 2017 at 19:46 Comment(2)
You can omit curly braces and return: pdfTestDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".txt"));Vitascope
Another solution: Files.list(Paths.get("your/path")).filter(path -> path.toString().endsWith(".txt")).collect(Collectors.toList());Repeated
A
31

Is there a specific reason you want to use FileNameExtensionFilter? I know this works..

private File[] getNewTextFiles() {
    return dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".txt");
        }
    });
}
Absence answered 22/4, 2011 at 1:6 Comment(0)
C
2

With java lambdas (available since java 8) you can simply convert javax.swing.filechooser.FileFilter to java.io.FileFilter in one line.

javax.swing.filechooser.FileFilter swingFilter = new FileNameExtensionFilter("jpeg files", "jpeg");
java.io.FileFilter ioFilter = file -> swingFilter.accept(file);
new File("myDirectory").listFiles(ioFilter);
Carnotite answered 1/6, 2017 at 9:22 Comment(1)
Thanks, this is especially useful if you need both a filechooser.FileFilter and a io.FileFilter but only want to define it once.Aspiration
G
1

Here's something I quickly just made and it should perform far better than File.getName().endsWith(".xxxx");

import java.io.File;
import java.io.FileFilter;

public class ExtensionsFilter implements FileFilter 
{
    private char[][] extensions;

    private ExtensionsFilter(String[] extensions)
    {
        int length = extensions.length;
        this.extensions = new char[length][];
        for (String s : extensions)
        {
            this.extensions[--length] = s.toCharArray();
        }
    }

    @Override
    public boolean accept(File file)
    {
        char[] path = file.getPath().toCharArray();
        for (char[] extension : extensions)
        {
            if (extension.length > path.length)
            {
                continue;
            }
            int pStart = path.length - 1;
            int eStart = extension.length - 1;
            boolean success = true;
            for (int i = 0; i <= eStart; i++)
            {
                if ((path[pStart - i] | 0x20) != (extension[eStart - i] | 0x20))
                {
                    success = false;
                    break;
                }
            }
            if (success)
                return true;
        }
        return false;
    }
}

Here's an example for various images formats.

private static final ExtensionsFilter IMAGE_FILTER = 
      new ExtensionsFilter(new String[] {".png", ".jpg", ".bmp"});
Grider answered 2/11, 2012 at 7:3 Comment(4)
Good, but my new mantra is to keep things simple unless they need to be more complicated, meaning, using Java API methods/classes unless I really have to write my own.Dishonest
@JeremyTrifilo Why have you done path[pStart - i] | 0x20 in the code?Chaldea
Why reinvent the wheel?Lubbi
The | 20 was for Uppercase vs Lowercase variations. It force adds 32 to the letter which makes it a lowercase letter for checking. So you could save a file as Example.PnG or Example.pNg and it would still pick it up. I don't normally redo stuff like this, but I do it because it's great learning if you can understand how these things work and what can be done to make them better. However I get what you mean it's redundant for this scenario as the performance increase is minimal and there isn't a need for this to be fast.Grider
D
0

Duh.... listFiles requires java.io.FileFilter. FileNameExtensionFilter extends javax.swing.filechooser.FileFilter. I solved my problem by implementing an instance of java.io.FileFilter

Edit: I did use something similar to @cFreiner's answer. I was trying to use a Java API method instead of writing my own implementation which is why I was trying to use FileNameExtensionFilter. I have many FileChoosers in my application and have used FileNameExtensionFilters for that and I mistakenly assumed that it was also extending java.io.FileFilter.

Dishonest answered 22/4, 2011 at 16:53 Comment(0)
B
0

Theres also DirectoryStream

import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
//import java.io.File;


Path path = Paths.get("foo/whereever");
// or if you still have a File-Object 'f':
// Path path = f.toPath();

try{
  Files.newDirectoryStream(path, "*.txt").forEach(System.out::println);
}catch(IOException e){}

Or if want to do something more complex

...
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.txt")) 
  for (Path entry: stream) {
    System.out.println(entry);
    //do something with entry 
  }
}catch(Exception e){
  e.printStackTrace();
}
Blowsy answered 13/1 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.