标签:
原文转自:http://www.tqcto.com/article/recommend/142.html
功能:删除某个目录及目录下的所有子目录和文件
知识点:File.delete()用于删除“某个文件或者空目录”!所以要删除某个目录及其中的所有文件和子目录,要进行递归删除,具体代码示例如下:
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(); } main(String[] args) { doDeleteEmptyDir("new_dir1"); String newDir2 = "new_dir2"; success = deleteDir( File(newDir2)); (success) { System.out.println("Successfully deleted populated directory: " + newDir2); } { System.out.println("Failed to delete populated directory: " + newDir2); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u014723529/article/details/48208695