In this tutorial we will go over FilenameFilter interface
to search a file and list of files with given file extension (i.e. .png, .jpg, .jpeg, .txt, .pdf).
lifeFiles
returns an array of abstract pathnames denoting 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 listFiles() method, except that the pathnames in the returned array must satisfy the filter.
If the given filter is null then all pathnames are accepted. Otherwise, a pathname satisfies the filter if and only if the value true results when the FilenameFilter.accept
(File, 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.
Some time back we have published a similar tutorial on how to search a file using java.nio.file package. Take a look at that too when you get a chance. java.nio.file defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.
Let’s get started:
Create CrunchifyFileSearchUsingFilenameFilter.java
package crunchify.com.tutorial; import java.io.File; import java.io.FilenameFilter; /** * @author Crunchify.com * In Java How to get list of files and search a file providing folder path? * FilenameFilter Interface Example */ public class CrunchifyFileSearchUsingFilenameFilter { public static void main(String[] args) { String crunchifyDirectory = "/Users/app/Download"; String crunchifyExt = ".txt"; crunchifySearchFilesWithExtension(crunchifyDirectory, crunchifyExt); } // FileNameFilter: Instances of classes that implement this interface are used to filter filenames. // These instances are used to filter directory listings in the list method of class File, and by the Abstract Window // Toolkit's file dialog component. public static class crunchifyFileNameFiler implements FilenameFilter { private final String crunchifyExt; public crunchifyFileNameFiler(String crunchifyExt) { // toLoverCase() Converts all of the characters in this String to lower case using the rules of the default locale. // This is equivalent to calling toLowerCase(Locale.getDefault()). this.crunchifyExt = crunchifyExt.toLowerCase(); } @Override public boolean accept(File crunchifyDirectory, String crunchifyName) { // endsWith(): Tests if this string ends with the specified suffix. return crunchifyName.toLowerCase().endsWith(crunchifyExt); } } private static void crunchifySearchFilesWithExtension(String crunchifyDirectory, String crunchifyExt) { // File class - Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname. File crunchifyFiles = new File(crunchifyDirectory); // exists(): Tests whether the file or directory denoted by this abstract pathname exists. if (!crunchifyFiles.exists()) crunchifyPrint(crunchifyDirectory + " Hmm.. Wrong Directory. Directory doesn't exists..." + crunchifyDirectory); // lifeFiles: Returns an array of abstract pathnames denoting 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 crunchifyListOfFiles() method, except that the pathnames in the returned array must satisfy the filter. // If the given filter is null then all pathnames are accepted. Otherwise, a pathname satisfies the filter if and only if the value true results when the FilenameFilter.accept(File, 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. File[] crunchifyListOfFiles = crunchifyFiles.listFiles(new crunchifyFileNameFiler(crunchifyExt)); assert crunchifyListOfFiles != null; int crunchifyCounter = 0; if (crunchifyListOfFiles.length == 0) { crunchifyPrint(crunchifyDirectory + "Hmm.. Sorry. No file exist with given file extension: " + crunchifyExt); } else { for (File crunchifyFile : crunchifyListOfFiles) { crunchifyCounter++; // separator: The system-dependent default name-separator character, represented as a string for convenience. // This string contains a single character, namely separatorChar. crunchifyPrint("File # " + crunchifyCounter + ": " + crunchifyDirectory + File.separator + crunchifyFile.getName()); } } } // Simple Print Utility private static void crunchifyPrint(String print) { System.out.println(print); } }
Just run above program as a Java Application.
IntelliJ IDEA console result:
File # 1: /Users/app/Download/task.txt File # 2: /Users/app/Download/robots_files_copy.txt File # 3: /Users/app/Download/robots_nio.txt File # 4: /Users/app/Download/robots_httpcomponents.txt File # 5: /Users/app/Download/robots.txt File # 6: /Users/app/Download/robots_commons_io.txt File # 7: /Users/app/Download/robots_stream.txt File # 8: /Users/app/Download/docs-cpt-crunchify.txt Process finished with exit code 0
Let me know if you face any issue running above Java Program.
Option-2) as mentioned above search file using java.nio.file package.
The post In Java How to get list of files and search files from given folder? java.io.FilenameFilter Interface Example appeared first on Crunchify.
0 Commentaires