I wanted to use the FilenameFilter used with the "File.list()"-method. I found a sample here.
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
/**
*
* @see: https://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter
*
* <h3>setup filter like:</h3>
* private static final ExtensionsFilenameFilter IMAGE_FILTER = <br/>
new ExtensionsFilenameFilter(".png", ".jpg", ".bmp");
*
*/
public class ExtensionsFilenameFilter implements FilenameFilter {
private char[][] extensions;
public ExtensionsFilenameFilter(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 dir, String name) {
char[] path = name.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;
}
}
Then I walk all pathes below a rootpath by:
public void walkList(String path) throws IOException {
List<String> imagefiles = new ArrayList<>();
Files.walk(Paths.get(path))
.filter(Files::isDirectory)
.map(walkPath -> {
List<String> pathnames = new ArrayList<>();
List<String> filenames = Arrays.asList(walkPath.toFile().list(new ExtensionsFilenameFilter("jpg", "tif", "bmp")));
filenames.forEach(fn -> {
pathnames.add(String.format("%s/%s", walkPath.toFile().toString(), fn));
});
return pathnames;
})
.forEach(imagefiles::addAll);
imagefiles.forEach(System.out::println);
}