package
org.learn;
import
java.io.File;
import
java.io.FilenameFilter;
public
class
ListImgVidTxtFiles {
public
static
void
main(String[] args) {
File directory =
new
File(
"d:/repo"
);
String[] videoFileFilters = {
"avi"
,
"wmv"
,
"mov"
};
String[] imageFileFilters = {
"jpg"
,
"png"
,
"tif"
};
String[] txtFileFilters = {
"txt"
};
System.out.println(
"1. Listing video files present in input directory"
);
printFiles(directory, videoFileFilters);
System.out.println(
"\n2. Listing image files present in input directory"
);
printFiles(directory, imageFileFilters);
System.out.println(
"\n3. Listing text files present in input directory"
);
printFiles(directory, txtFileFilters);
}
private
static
void
printFiles(File inputDirectory, String[] filters) {
File[] files = inputDirectory.listFiles(
new
FileFilter(filters));
for
(File file : files) {
System.out.println(file.getName());
}
}
}
class
FileFilter
implements
FilenameFilter {
private
String[] allowedExtensions =
null
;
public
FileFilter(String[] allowedExtensions) {
this
.allowedExtensions = allowedExtensions.clone();
}
@Override
public
boolean
accept(File dir, String name) {
String fileExt = name.substring(name.lastIndexOf(
"."
) +
1
);
for
(String imgExt : allowedExtensions) {
if
(imgExt.compareToIgnoreCase(fileExt) ==
0
)
return
true
;
}
return
false
;
}
}