In this example we are going to see how to delete a file in Java. Of course Java offers a very convenient API to perform deletion and creation. Most of them are placed in File
class. We are going to use delete()
methods that deletes the file or directory. If the file is deleted successfully, the methods returns true
, else false
. In order to delete a directory with this method, the directory has to be empty.
1. Using delete()
Here is how you can use File.delete() to delete a file.
JavaDeleteFileExample.java:
package com.javacodegeeks.core.file; import java.io.File; public class JavaDeleteFileExample { private static final String FILE_PATH = "F:\\nikos7\\Desktop\\testFile.txt"; public static void main(String[] args){ if( JavaDeleteFileExample.deleteFile(FILE_PATH) ){ System.out.println("File is deleted!"); } else { System.err.println("Failed to delete file."); } } public static boolean deleteFile(String filePath){ File file = new File(FILE_PATH); return file.delete(); } }
2. Using NIO Files.delete
Most of the times you need not look any further for other ways to delete a file. But there is one problem with the above method. In case of an error, it is not informative enough about the reason of the failure. As you can see, it does not through any exceptions in case of an error. NIO’s Files.delete(Path path)
method offers that extremely useful feature.
Let’s see how you can use it :
JavaDeleteFileExample.java:
package com.javacodegeeks.core.file; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class JavaDeleteFileExample { private static final String FILE_PATH = "F:\\nikos7\\Desktop\\testFile.txt"; public static void main(String[] args){ try { JavaDeleteFileExample.deleteFileNIO(FILE_PATH); } catch (IOException e) { e.printStackTrace(); } } public static void deleteFileNIO(String filePath) throws IOException{ Path path = Paths.get(filePath); Files.delete(path); } }
Now when I run this program and "F:\\nikos7\\Desktop\\testFile.txt"
does not exist in my system, here is the output I get :
java.nio.file.NoSuchFileException: F:\nikos7\Desktop\testFile.txt at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source) at sun.nio.fs.AbstractFileSystemProvider.delete(Unknown Source) at java.nio.file.Files.delete(Unknown Source) at com.javacodegeeks.core.file.JavaDeleteFileExample.deleteFileNIO(JavaDeleteFileExample.java:36) at com.javacodegeeks.core.file.JavaDeleteFileExample.main(JavaDeleteFileExample.java:17)
So you get a very good idea of the reason your program failed. Of course, exception handling has many other advantages like error reporting, logging etc.
Download the Source Code
This was an example on how to delete a file in Java.
You can download the full source code of this example here : JavaDeleteFileExample.zip
Leave a Reply