Translated from: http://www.tqcto.com/article/recommend/142.html
Function: Delete all subdirectories and files in a directory and directory
knowledge point: File.delete () used to remove "A file or empty directory" ! So to delete a directory and all the files and subdirectories in it, recursively delete the code example as follows:
java.io.File; DeleteDirectory { dodeleteemptydir (String dir) { success = ( file (dir)). Delete (); (Success) { system.out.println (" successfully deleted empty directory: " + dir); } { System.out.println ("failed to delete empty directory: " + dir); } } deletedir (File dir) { ( Dir.isdirectory ()) { string[] children = dir.list (); ( i=0; i< children.length; i++) { success = deletedir ( file (dir, children[i)); (!success) { ; } } } dir.delete (); &Nbsp; } main (String[] args) { dodeleteemptydir ("New_dir1"); String newDir2 = "New_dir2"; success = deletedir ( file (NEWDIR2)); (Success) { System.out.println ("successfully deleted populated directory: " &NBSP;+&NBSP;NEWDIR2); } { system.out.println ("failed to delete populated directory: " &NBSP;+&NBSP;NEWDIR2); } }}
From the original: programming technology
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Delete files in Java, delete directories and all files under directory