How to delete a directory?
SolutionFollowing example demonstares how to delete a directory after deleting its files and directories by the use ofdir.isDirectory(),dir.list() and deleteDir() methods of File class.
import java.io.File; public class Main { public static void main(String[] argv) throws Exception { deleteDir(new File("c:\\temp")); } public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir (new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); System.out.println("The directory is deleted."); } }Result
The above code sample will produce the following result.
The directory is deleted.
java_directories.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4