Java

Java FilenameFilter

The listFiles() function in Java can be employed to search the file names using the FilenameFilter interface. The File instances in a location are returned if the listFiles() is invoked without any arguments. We can choose to retrieve a portion of those instances if we provide a filter as an argument. We must define the accept() function to create an instance that inherits the  FilenameFilter. The file’s home path is represented by the dir entity and its title by the filename. The file is presented in the collection of File instances out from call to listFiles() if accept() yields true. Otherwise, the call to listFiles will not deliver the file(). 

Install Java

Before starting with our Java example, we open the shell application from the Application area of the Linux machine. This is because we use the shell console application to install the Java Runtime Environment. For its installation, it is necessary to use the “apt” package with the “sudo” privilege in the “install” instruction. For Java RunTime Environment installation, use the package name “default-jre” as presented:

Example

We use our first Java example to show how a FilenameFilter interface can be used to list all the specific files from the specified directory of our Linux system. For this, we must have some specific type files in the working directory. First, we create a Java file named “filter.java” that is used for coding. After this, we create two text files, hello.txt and new.txt, respectively. All three files are created with the “touch” query. Now, the use of the “ls” instruction presents all three files in the home folder.

Starting with the first Java example code, we make it as simple as possible. It starts with the import of Java’s main packages to utilize the file handling. We import the File and IOExecption class of the java.io package along with the import of its FilenameFilter interface that are utilized to get a list of specific files. First, importing all the required packages, we create a demo “Test” class and add the main() function implementation to it. As the program starts its execution, the file object “fobj” is generated using the File() class of a Java, taking the home directory path as its argument to the constructor function.

The FilenameFilter() interface is used here by creating its object “tf” that is used to filter the file names. The Boolean type public built-in function “accept” is here to search and match for the specific file names. It uses the directory and name of a file to be searched in its parameters from the File class. While the names of all the matched patterns are converted to lower case using the toLowerCase() function and  are saved to a string variable “n”. The “if” statement is castoff to perform the main thing – checking for the file suffix. It uses the variable “n” value to match its end with the specific pattern via the “endsWith” function of Java.

If the end characters of a variable “n” value match with the specified file name pattern, it returns “true”. Otherwise, it returns false. We specify the “.txt” to check for all the text files. Meanwhile, we create an array object “lf” using the File() class. And we store all the matched files in it using the list files() function of a Java that is called by the file object “fobj” by passing the text filter object “tf”. The “for” loop is initialized until the end of an array “lf” for each file “f”. The “if” statement checks if the file “f” is a directory or a simple file via the isDirectory() function. It displays the resultant path according to that.

For that, it utilizes the println() function of a “System.out” class first in the “if” statement to display the “Directory” or “File”. After this, we use the System.out.println() statement again to get the name path to the file using the getCanonicalPath() function in it. We save this Java file before the execution process.

After executing the Java file for the “.txt” file patterns, we get a total of 2 paths listed for the files of text types in the home working directory.

You can also utilize the very same method to match and display the matched file name patterns within a folder other than the current working directory. Let’s say, we have the same two text files in the Documents folder of our system as per the “ls” query.

Now, you need to update the path to the directory where we match or search the file name pattern. So, we provide the path to the Documents folder and save it.

Upon execution, it again displays the path to 2 text files – hello.txt and new.txt.

In a very similar way, you can search for other file types as well. Let’s say, we want to search for all the “.java” files in the home directory of our Linux system. Replace “.txt” with “.java”.

The execution shows the path to the Java file “filter.java” which we have been working on.

Sometimes, the pattern that we search for matches the directory name. For instance, we add the “s” pattern to be searched in the home folder.

In return, the execution shows the paths to the folders in a home directory which contains “s” at their end.

Conclusion

We explained the use of the FilenameFilter() interface of Java to filter all the file paths that match the specific pattern in our article. For this, we made sure that the Java environment is fully configured in our Linux operating system. We also explained the Java example using the FilenameFilter interface to create a filter object to filter out the specific file paths containing a pattern. Using this example, we filtered the text files, java files, and specific directories from the home working directory and the other folders as well.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.