I am selecting files in java using the following code:
File folder = new File("path to folder");
File[] listOfFiles = folder.listFiles();
Now what to do if I want to select only image files?
Use one of the versions of File.listFiles()
that accepts a FileFilter
or FilenameFilter
to define the matching criteria.
For example:
File[] files = folder.listFiles(
new FilenameFilter()
{
public boolean accept(final File a_directory,
final String a_name)
{
return a_name.endsWith(".jpg");
// Or could use a regular expression:
//
// return a_name.toLowerCase().matches(".*\\.(gif|jpg|png)$");
//
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With