June 24, 2022

Java Program to Delete Files Having a Specific Extension

In this article we’ll see a Java program to delete all the files having a specific extension from a directory.

The Java program to delete all the files with the certain extension works as given below-

  1. Implement FileNameFilter interface to list files with the given extension.
  2. Iterate through those files to delete them using the delete method.

FileNameFilter Interface in Java

This section gives some background on the FileNameFilter interface and what needs to be done for implementing this interface. In FilenameFilter interface there is a method accept().

accept(File dir, String name)- Tests if a specified file should be included in a file list.

By implementing this method you can test each file in the passed directory. If file has a required extension then it is included otherwise discarded.

Another point is how to include the files with the given extension in a list, for that there is a File.list() method that takes instance of FilenameFilter.

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.

Java program to delete files with specific extension

FileNameFilter is a functional interface that means it can also be implemented as a lambda expression. Following Java program implements FileNameFilter as a lambda expression. In the program files having “.bckup” extension are deleted.

public class DeleteFilteredFiles {
  public static void main(String[] args) throws IOException {
    // Folder from which files are fetched
    File file = new File("F:\\knpcode");
    // file Extension 
    String extension = ".bckup";
    // Implemented as lambda. filter all the files
    // having passed extension
    File[] fileList = file.listFiles((d,f)-> f.toLowerCase().endsWith(extension));
              
    // Delete all the included files
    for(File f : fileList) {
      System.out.println(f.getAbsolutePath());
      if(!f.delete())
        throw new IOException("Not able to delete file: " + f.getAbsolutePath());
    }
  }
}

If you don’t want to use lambda expression same thing can be done by implementing FileNameFilter as an anonymous class.

public class DeleteFilteredFiles {
  public static void main(String[] args) throws IOException {
    // Folder from which files are fetched
    File file = new File("F:\\knpcode");
    // file Extension 
    String extension = ".bckup";
    // Implemented as lambda. filter all the files
    // having passed extension
    File[] fileList = file.listFiles(new FilenameFilter() {			
      @Override
      public boolean accept(File dir, String name) {
        if(name.toLowerCase().endsWith(extension))
          return true;
        else 
          return false;
      }
    });
              
    // Delete all the included files
    for(File f : fileList) {
      System.out.println(f.getAbsolutePath());
      if(!f.delete())
        throw new IOException("Not able to delete file: " + f.getAbsolutePath());
    }
  }
}

That's all for the topic Java Program to Delete Files Having a Specific Extension. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment