Java Program to Find/Delete Files Based on Extensions
Last Updated :
10 Aug, 2021
We can use FilenameFilter in Java to override the accept(File dir, String name) method to execute the file filtering operation. Filename interface has a method accept(). It checks whether a given file should be included in a list of files. We can check each file in the passed directory by implementing this method. If the file has the required extension, then it is included otherwise discarded.
Syntax:
accept(File dir, String name)
Now the next things that come into play is how to include the files in a list with the given extension, for that there is a File.list() method that takes the FilenameFilter instance. the list Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfies the specified filter. So let us discuss this function in detail to get a better understanding while dealing with the files.
The list() method is a part of File class. The function returns an array of the string denoting the files in a given abstract pathname if the pathname is a directory else returns null. The function is an overloaded function. One of the function does not have any parameter and the other function takes FilenameFilter object as a parameter as described below
Function Signature:
public String[] list()
public String[] list(FilenameFilter f)
Function Syntax:
file.list()
file.list(filter)
Parameters: The function is an overloaded function. One of the function does not have any parameter and the other function takes FilenameFilter object as a parameter
Return value: The function returns a string array, or a null value if the file object is a file.
Exception: This method throws Security Exception if the function is not allowed to write access to the file.
Procedure:
We will follow two steps to delete files based on extensions:
- Implement the FileNameFilter interface to list files with the given extension.
- Iterate through those files to delete them using the delete() method.
Example:
Java
import java.io.*;
class GFG {
private static final String FILE_DIRECTORY
= "/Users/mayanksolanki/Desktop/" ;
private static final String FILE_EXTENSION = ".jpeg" ;
public void deleteFile(String folder, String extension)
{
FileExtFilter filter = new FileExtFilter(extension);
File direction = new File(folder);
String[] list = direction.list(filter);
for ( int i = 0 ; i < list.length; i++) {
System.out.println(list[i]);
}
if (list.length == 0 )
return ;
File fileDelete;
for (String file : list) {
String temp = new StringBuffer(FILE_DIRECTORY)
.append(File.separator)
.append(file)
.toString();
fileDelete = new File(temp);
boolean isdeleted = fileDelete.delete();
System.out.println( "file : " + temp
+ " is deleted : "
+ isdeleted);
}
}
public static void main(String args[])
{
new GFG().deleteFile(FILE_DIRECTORY, FILE_EXTENSION);
}
}
class FileExtFilter implements FilenameFilter {
private String extension;
public FileExtFilter(String extension)
{
this .extension = extension;
}
public boolean accept(File directory, String name)
{
return (name.endsWith(extension));
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...