- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to filter the files by file extensions and show the file names in Java?
The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.
To get the list of all the existing files in a directory this class provides five different methods to get the details of all files in a particular folder −
- String[] list()
- File[] listFiles()
- String[] list(FilenameFilter filter)
- File[] listFiles(FilenameFilter filter)
- File[] listFiles(FileFilter filter)
Among these, the String[] list(FilenameFilter filter) method returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter.
The FilenameFilter is an interface in Java with a single method.
accept(File dir, String name)
To get the file names based on extensions implement this interface as such and pass its object to the above specified list() method of the file class.
Example
Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories as −
The following Java program prints the names of the text files and jpeg files in the path D:\ExampleDirectory separately.
import java.io.File; import java.io.FilenameFilter; import java.io.IOException; public class ListOfFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\ExampleDirectory"); FilenameFilter textFilefilter = new FilenameFilter(){ public boolean accept(File dir, String name) { String lowercaseName = name.toLowerCase(); if (lowercaseName.endsWith(".txt")) { return true; } else { return false; } } }; FilenameFilter jpgFilefilter = new FilenameFilter(){ public boolean accept(File dir, String name) { String lowercaseName = name.toLowerCase(); if (lowercaseName.endsWith(".jpg")) { return true; } else { return false; } } }; //List of all the text files String textFilesList[] = directoryPath.list(textFilefilter); System.out.println("List of the text files in the specified directory:"); for(String fileName : textFilesList) { System.out.println(fileName); } String imageFilesList[] = directoryPath.list(jpgFilefilter); System.out.println("List of the jpeg files in the specified directory:"); for(String fileName : imageFilesList) { System.out.println(fileName); } } }
Output
List of the text files in the specified directory: SampleFile1.txt SampleFile2.txt SapmleFile3.txt List of the jpeg files in the specified directory: cassandra_logo.jpg cat.jpg coffeescript_logo.jpg javafx_logo.jpg
- Related Articles
- How to search a directory with file extensions in Java?
- How to find all the distinct file extensions in a folder hierarchy (Linux)?
- How to open multiple filenames in Tkinter and add the file names to a list?
- How to read all files in a folder to a single file using Java?
- What do the python file extensions, .pyc .pyd .pyo stand for?
- Java program to merge two files into a third file
- How to find the file using Java?
- How to move a file, group of files, and directories in Linux?
- How to read data from one file and print to another file in Java?
- Get only the file extension from a column with file names as strings in MySQL?
- How to use external “.js” files in an HTML file?
- How to save files using a File Chooser in JavaFX?
- C# Program to display temporary file names
- How to exclude specific file names using Get-ChildItem in PowerShell?
- How to open multiple files using a File Chooser in JavaFX?
