Home » Core Java » io » File » Delete files with certain extension only using FilenameFilter in Java

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Delete files with certain extension only using FilenameFilter in Java

In this tutorial we are going to show you how to use the FilenameFilter interface in Java in order to list all the file with a certain property in their names. In this example for instance, we want to delete all the text files in a specific directory, hence the files ending with “.txt” extension. 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.

In short to delete files with certain extensions using FilenameFilter you have to:

  • Create a class that implements the FilenameFilter interface and override the accept method to perform the filtering you want in the filename.
  • Use the above class as the argument in the list method, when listing the files of the target directory.

So here is the FileFilter class the implements FilenameFilter interface:

FileFilter.java

package com.javacodegeeks.java.core;

import java.io.File;
import java.io.FilenameFilter;

public class FileFilter implements FilenameFilter {

	private String fileExtension;

	public FileFilter(String fileExtension) {
		this.fileExtension = fileExtension;
	}

	@Override
	public boolean accept(File directory, String fileName) {
		return (fileName.endsWith(this.fileExtension));
	}
}

DeleteTextFiles.java

package com.javacodegeeks.java.core;

import java.io.File;

public class DeleteTextFiles {

	private static String parentDirectory = "C:\\Users\\nikos7\\Desktop\\files";
	private static String deleteExtension = ".txt";

	public static void main(String[] args) {
		FileFilter fileFilter = new FileFilter(deleteExtension);
		File parentDir = new File(parentDirectory);

		// Put the names of all files ending with .txt in a String array
		String[] listOfTextFiles = parentDir.list(fileFilter);

		if (listOfTextFiles.length == 0) {
			System.out.println("There are no text files in this direcotry!");
			return;
		}

		File fileToDelete;

		for (String file : listOfTextFiles) {

			//construct the absolute file paths...
			String absoluteFilePath = new StringBuffer(parentDirectory).append(File.separator).append(file).toString();

			//open the files using the absolute file path, and then delete them...
			fileToDelete = new File(absoluteFilePath);
			boolean isdeleted = fileToDelete.delete();
			System.out.println("File : " + absoluteFilePath + " was deleted : " + isdeleted);
		}
	}
}

Output:

File : C:\Users\nikos7\Desktop\files\file1.txt was deleted : true
File : C:\Users\nikos7\Desktop\files\file2.txt was deleted : true
File : C:\Users\nikos7\Desktop\files\file3.txt was deleted : true
File : C:\Users\nikos7\Desktop\files\file4.txt was deleted : true
File : C:\Users\nikos7\Desktop\files\file5.txt was deleted : true

 
This was an example on how to delete files with certain extension only using FilenameFilter in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of