Java delete file
In this article, we will take a look at 2 different ways to delete a file in java. At the end, we will also understand how to delete multiple files in a folder and delete files with some particular extension in a directory.
java.io.File
class has a delete()
method which deletes a file at the location the file object points to.delete()
returns true
, if the file is successfully deleted and false otherwise. Example, File f = new File("d:\\delete.txt"); if(f.delete()) { System.out.print("File deleted"); } else { System.out.print("Unable to delete file"); }
If the file object points to a directory, then it must be empty in order to be deleted. If the directory is non-empty, no error will be thrown, but it will not be deleted either.
If the file at the specified path does not exist, there will be no error.
Method 2: Files.delete()
java.nio.file.Files
class was added in java 7 and provides static method to deal with files.
This class has a delete()
method which accepts an object of java.nio.file.Path
as argument and deletes the file at that location. Example,
Path path = Paths.get("d:\\delete.txt"); try { Files.delete(path); } catch(IOException e) { e.printStackTrace(); }
delete()
method
1. throws IOException
and thus, it needs to be surrounded with try-catch block.
2. throws NoSuchFileException
if the file does not exist at the given path.
3. throws DirectoryNotEmptyException
if the path denotes a directory, which is non-empty at the time of deletion.
java.nio.file.Files
class has a deleteIfExists()
method. It is similar to delete()
except that it does not throw an error if the file does not exist. Example, Path path = Paths.get("d:\\delete.txt"); try { Files.deleteIfExists(path); } catch(IOException e) { e.printStackTrace(); }
deleteIfExists()
method
1. throws IOException
and thus, it needs to be surrounded with try-catch block.
2. throws DirectoryNotEmptyException
if the path denotes a directory, which is non-empty at the time of deletion.
Delete files in a directory
java.io.File
class has a listFiles()
method which returns an array of files in a directory.
To delete all the files in a directory, iterate over this array with a for-each loop as shown below
File dir = new File("d:\\work"); // get files in folder File[] files = dir.listFiles(); // iterate file list for(File file : files) { file.delete(); }
listFiles()
will return an empty array if the directory is empty and null
, if the path does not represent a directory.
Delete files with extension
There are two approaches to delete all files in a directory with a certain extension.
1. Check for extension
Get the list of all files with listFiles()
method. Iterate over this array and in every iteration, check if the extension of current file matches with the extension that we wish to delete. Example,
File folder = new File("d:\\work"); File[] files = folder.listFiles(); for(File file : files) { // check file extension if(file.getName().endsWith(".txt")) { file.delete(); } }
2. Using FileNameFilter
Supply a FileNameFilter
to listFiles()
method.
FileNameFilter
is used to filter file names based on a condition. It is an interface having accept()
method which returns a boolean value.
Files for which accept()
returns true
will be included in the array returned by listFiles()
. This means that while iterating over the array, we are not required to check for the extension. Example,
File folder = new File("d:\\work"); File[] files = folder.listFiles(new FileNameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".txt"); } }); for(File file : files) { file.delete(); }
Note that FileNameFilter
is an interface, so it can be implemented as an anonymous inner class as shown in the above example.
accept()
method is invoked for every file in listFiles()
method. Only the file that satisfies the condition in accept()
is added to the result array.
FileNameFilter
is a functional interface, meaning that it has only one abstract method. So, entire definition of FileNameFilter
can be represented as a java 8 Lambda expression as shown below
File folder = new File("d:\\work"); File[] files = folder.listFiles((d, n) -> n.endsWith(".txt")); for(File file : files) { file.delete(); }