Delete a file in Java
This post will discuss how to delete a file in Java.
There are several ways to delete a file in plain Java and using third-party libraries. These are discussed below in detail:
1. Using File Class
Before Java 7, we can use the File.delete() method for deleting a given file. The method returns true if the file is successfully deleted and returns false if the file doesn’t exist or in case of an I/O error. This method doesn’t throw an IOException when a file deletion fails.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.File; class Main { public static void main(String[] args) { File file = new File("foo.txt"); boolean result = file.delete(); if (result) { System.out.println("File is successfully deleted."); } else { System.out.println("File deletion failed."); } } } |
2. Using NIO
Starting from Java 7, we can use the deleteIfExists() method of the Files class that deletes a file if it exists. It returns true if the file was deleted by this method, and an IOException is thrown if an I/O error occurs. If the file doesn’t exist, false is returned by the function, but no exception is thrown.
|
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 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path path = Paths.get("foo.txt"); try { boolean result = Files.deleteIfExists(path); if (result) { System.out.println("File is successfully deleted."); } else { System.out.println("File deletion failed."); } } catch (IOException e) { e.printStackTrace(); } } } |
The Files class defines the delete method to throw an IOException when a file cannot be deleted, which is very useful for error reporting and to diagnose why a file cannot be deleted.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path path = Paths.get("foo.txt"); try { Files.delete(path); System.out.println("File is successfully deleted."); } catch (IOException e) { System.out.println("File deletion failed."); e.printStackTrace(); } } } |
3. Using Apache Commons IO
FileUtils class from Apache Commons IO library has the forceDelete() method to delete a file. If the file already exists, then java.io.FileNotFoundException is thrown, and IOException is thrown if deletion is unsuccessful.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("foo.txt"); try { FileUtils.forceDelete(file); System.out.println("File is successfully deleted."); } catch (IOException e) { System.out.println("File deletion failed."); e.printStackTrace(); } } } |
That’s all about deleting a file in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)