Listing Select Files in a Directory

Java has long provided a variation of the list method to cherry-pick filenames. This version of list takes a FilenameFilter as its parameter. This interface has one method, accept, that takes two parameters: File dir (representing the directory) and String name (representing a filename). We’d return a true from the accept method to include the given filename in the list, and false otherwise. Let’s explore the options to implement this method.

It’s a habitual practice in Java to pass to the list method an instance of an anonymous inner class that implements the FilenameFilter interface. For example, let’s look at how we’d select only the java files in a fpij directory using that approach.

compare/fpij/ListSelectFiles.java ...

Get Functional Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.