Java when deleting a file or folder, under the Java.io.File class has a Delete method, and can return TRUE or FALSE, this method to delete a single file is good, but when the folder is deleted, if there are files or subfolders under the folder, Call this method will return false, that is, delete the failure, this method can only delete empty folders, if so, the trouble, to achieve the purpose of deleting folders, you have to delete a layer, it is clear that you can use recursion, realize as follows: (file or folder are good!!!) If the Linux system is running, there may be a small place to change, why change to think! )
Package Com.demo;import Java.io.file;public class Test10 {public boolean delete (String path) {File File = new file (path); if (!file.exists ()) {return false;} if (File.isfile ()) {return file.delete ();} file[] files = file.listfiles (); for (file f:files) {if (F.isfile ()) {if (!f.delete ()) {System.out.println ( F.getabsolutepath () + "Delete error!"); return false;}} Else{if (!this.delete (F.getabsolutepath ())) {return false;}}} return File.delete ();} public static void Main (string[] args) {Test10 t = new Test10 (); System.out.println (T.delete ("c:\\1"));}}
Java Delete files and folders