Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
public String[] list(FilenameFilter filter)
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. The behavior of this method is the same as that of the list() method, except that the strings in the returned array must satisfy the filter. If the given filter is null then all names are accepted. Otherwise, a name satisfies the filter if and only if the value true results when the FilenameFilter.accept(java.io.File, java.lang.String) method of the filter is invoked on this abstract pathname and the name of a file or directory in the directory that it denotes.
Parameters:
filter - A filename filter
Returns:
An array of strings naming the files and directories in the directory denoted by this abstract pathname that were accepted by the given filter. The array will be empty if the directory is empty or if no names were accepted by the filter. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the directory
import java.util.*;
public class DirectoryFilter implements java.io.FilenameFilter {
/* List of Strings */
private List acceptedFileExtension;
/* Accepts a list of Strings for file extensions */
DirectoryFilter(List acceptedFileExtension) {
this.acceptedFileExtension = acceptedFileExtension;
}
public boolean accept(File dir, String name) {
/* Early out if name is null */
if (name == null) {
return false;
}
String fileExtension = getFileExtension(name);
if (fileExtension != null && acceptedFileExtension.contains(fileExtension)) {
return true;
}
else {
return false;
}
}
private String getFileExtension(String name) {
int index = name.lastIndexOf('.');
if (index != -1) {
return name.substring(index+1).trim();
}
else {
return null;
}
}
}
/* Create File Object */
String path = "Your Directory Path";
File directory = new File(path);
/* Create DirectoryFilter */
ArrayList fileTypes = new ArrayList(1); //initial capacity 1
fileTypes.add("txt");
DirectoryFilter fileExtensionFilter = new DirectoryFilter(fileTypes);
/* Get Filtered Directory Listing */
String fileList[] = directory.list(fileExtensionFilter);
<%@ page import="java.util.*, java.io.FilenameFilter" %>
<%!
FilenameFilter createDirectoryFilter(final List FileTypes) {
/* BEGIN INNER CLASS DEFINITION */
class DirectoryFilter implements FilenameFilter {
List acceptedFileExtension = FileTypes;
public boolean accept(File dir, String name) {
/* Early out if name is null */
if (name == null) {
return false;
}
String fileExtension = getFileExtension(name);
if (fileExtension != null && acceptedFileExtension.contains(fileExtension)) {
return true;
}
else {
return false;
}
}
private String getFileExtension(String name) {
int index = name.lastIndexOf('.');
if (index != -1) {
return name.substring(index+1).trim();
}
else {
return null;
}
}
} /* END OF INNER CLASS DEFINITION */
return new DirectoryFilter();
}
%>
<%
/* Create File Object */
String path = "Your Directory Path";
File directory = new File(path);
/* Create DirectoryFilter */
ArrayList fileTypes = new ArrayList(1); //initial capacity 1
fileTypes.add("txt");
FilenameFilter fileExtensionFilter = createDirectoryFilter(fileTypes);
/* Get Filtered Directory Listing */
String fileList[] = directory.list(fileExtensionFilter);
%>