Today we will look into Java delete file and java delete directory examples. Earlier we learned how to create a file in java.
Java delete file
- Java File delete() method can be used to delete files or empty directory/folder in java. Java file delete method returns true if file gets deleted and returns false if file doesn’t exist.
- If you are trying to delete a directory, it checks java File delete() method check if it’s empty or not. If directory is empty, it gets deleted else
delete()
method doesn’t do anything and return false. So in this case, we have to recursively delete all the files and then the empty directory. - Another way to delete a non-empty directory is by using
Files.walkFileTree()
method. In this method, we can process all the files one by one, and call delete method on single files.
Java delete file example
Let’s see java delete file example program.
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 |
package com.journaldev.files; import java.io.File; public class DeleteFileJava { /** * This class shows how to delete a File in Java * @param args */ public static void main(String[] args) { //absolute file name with path File file = new File("/Users/pankaj/file.txt"); if(file.delete()){ System.out.println("/Users/pankaj/file.txt File deleted"); }else System.out.println("File /Users/pankaj/file.txt doesn't exist"); //file name only file = new File("file.txt"); if(file.delete()){ System.out.println("file.txt File deleted from Project root directory"); }else System.out.println("File file.txt doesn't exist in the project root directory"); //relative path file = new File("temp/file.txt"); if(file.delete()){ System.out.println("temp/file.txt File deleted from Project root directory"); }else System.out.println("File temp/file.txt doesn't exist in the project root directory"); //delete empty directory file = new File("temp"); if(file.delete()){ System.out.println("temp directory deleted from Project root directory"); }else System.out.println("temp directory doesn't exist or not empty in the project root directory"); //try to delete directory with files file = new File("/Users/pankaj/project"); if(file.delete()){ System.out.println("/Users/pankaj/project directory deleted from Project root directory"); }else System.out.println("/Users/pankaj/project directory doesn't exist or not empty"); } } |
Below is the output when we execute the above java delete file example program for the first time.
1 2 3 4 5 6 7 |
<span style="color: #008000;"><strong><code> /Users/pankaj/file.txt File deleted file.txt File deleted from Project root directory temp/file.txt File deleted from Project root directory temp directory deleted from Project root directory /Users/pankaj/project directory doesn't exist or not empty </code></strong></span> |
Note that temp directory had file.txt and it got deleted first and then directory was empty and got deleted successfully, /Users/pankaj/project was not empty and hence not deleted.
In the subsequent run of the same program, the output is:
1 2 3 4 5 6 7 |
<span style="color: #008000;"><strong><code> File /Users/pankaj/file.txt doesn't exist File file.txt doesn't exist in the project root directory File temp/file.txt doesn't exist in the project root directory temp directory doesn't exist or not empty in the project root directory /Users/pankaj/project directory doesn't exist or not empty </code></strong></span> |
Notice that unlike createNewFile(), delete method doesn’t throw IOException.
Java delete directory
Below is a simple program showing how to delete a non-empty directory. This will work if your directory contains files only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.journaldev.files; import java.io.File; public class JavaDeleteDirectory { public static void main(String[] args) { File dir = new File("/Users/pankaj/log"); if(dir.isDirectory() == false) { System.out.println("Not a directory. Do nothing"); return; } File[] listFiles = dir.listFiles(); for(File file : listFiles){ System.out.println("Deleting "+file.getName()); file.delete(); } //now directory is empty, so we can delete it System.out.println("Deleting Directory. Success = "+dir.delete()); } } |
Java delete directory recursively
Earlier we had to write recursion based code to delete a directory with nested directories. But with Java 7, we can do this using Files class. Below is the code that you should use to delete a directory. It takes care of deleting nested directories too.
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 |
package com.journaldev.files; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class JavaDeleteDirectoryRecursively { public static void main(String[] args) throws IOException { Path directory = Paths.get("/Users/pankaj/log"); Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { Files.delete(file); // this will work because it's always a File return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); //this will work because Files in the directory are already deleted return FileVisitResult.CONTINUE; } }); } } |
That’s all for java delete file and java delete directory examples.
Reference: Java NIO Files Class API Doc