Sometimes you need to delete files having certain extension within a directory. Two approach are possible, you can either list all files within the directory and check their extension before deletion or list only files having the extension using filename Filter and delete all of them.
This article shows this two approaches:
1- FilenameFilter
JDK 1.1+
Java provides the interface java.io.FilenameFilter to filter filenames. Instances of classes that implement it are used to filter directory listings in the list method of class File.
First thing to do is to create our extension filter that will only accept files ending with certain extension while listing. Below the extension filter :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * Filtering file according to there extension. * * @author Radouane ROUFID. * */ public class ExtensionFilter implements FilenameFilter { private String extension; public ExtensionFilter(String extension) { this.extension = extension; } /** * Accepting only files ending with the extension. */ public boolean accept(File dir, String name) { return name.endsWith(extension); } } |
This filter is used to list files within the directory as below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import java.io.File; import java.io.IOException; import com.roufid.tutorials.filter.ExtensionFilter; /** * Delete files within a directory. * * @author Radouane ROUFID. * */ public class DeleteFileWithExtensionFilter { public static void main(String[] args) { // Directory containing files to delete. String directory = "C:/dev/files/"; // Extension. String extension = ".txt"; try { deleteFileWithExtension(directory, extension); } catch (IOException e) { System.out.println("Problem occurs when deleting files"); e.printStackTrace(); } } /** * Deleting files with an extension within a directory. * * @param directory * Path to the directory where files to delete are located. * @param extension * Extension of files to delete. * @throws IOException * Thrown exception if can't delete a file. */ public static void deleteFileWithExtension(String directory, String extension) throws IOException { File dir = new File(directory); // Listing only files having the extension. File[] filesToDelete = dir.listFiles(new ExtensionFilter(extension)); // Using the custom filenameFilter : ExtensionFilter. for(File file : filesToDelete) { if(!file.delete()) { throw new IOException(); } } } } |
2- Using native Java
JDK 1.1+
If you want to list all files within the directory and check their extension before the deletion, below the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.io.File; import java.io.IOException; /** * Delete files within a directory having certain extension with native Java. * * @author Radouane ROUFID. * */ public class DeleteFileWithExtension { public static void main(String[] args) { // Directory containing files to delete. String directory = "C:/dev/files/"; // Extension. String extension = ".txt"; try { deleteFileWithExtension(directory, extension); } catch (IOException e) { System.out.println("Problem occurs when deleting files"); e.printStackTrace(); } } /** * Deleting files with an extension within a directory. * * @param directory * Path to the directory where files to delete are located. * @param extension * Extension of files to delete. * @throws IOException * Thrown exception if can't delete a file. */ public static void deleteFileWithExtension(String directory, String extension) throws IOException { File dir = new File(directory); for (File file : dir.listFiles()) { if (file.getName().endsWith(extension) && !file.delete()) { throw new IOException(); } } } } |