ImportJava.io.File; Public classtest{ Public Static voidMain (String args[]) {Test T=NewTest (); Delfolder ("C:/bb"); System.out.println ("Deleted");}//Delete a folder//param folderpath Folder complete absolute path Public Static voidDelfolder (String folderpath) {Try{delallfile (folderpath);//Delete all content insideString FilePath =FolderPath; FilePath=filepath.tostring (); Java.io.File Myfilepath=NewJava.io.File (FilePath); Myfilepath.delete (); //Delete Empty folders}Catch(Exception e) {e.printstacktrace (); }}//Delete all files under the specified folder//param path Folder complete absolute path Public Static Booleandelallfile (String path) {BooleanFlag =false; File File=NewFile (path); if(!file.exists ()) { returnFlag; } if(!file.isdirectory ()) { returnFlag; } string[] Templist=file.list (); File Temp=NULL; for(inti = 0; i < templist.length; i++) { if(Path.endswith (file.separator)) {temp=NewFile (path +Templist[i]); } Else{Temp=NewFile (path + file.separator +Templist[i]); } if(Temp.isfile ()) {temp.delete (); } if(Temp.isdirectory ()) {Delallfile (path+ "/" + templist[i]);//Delete the files inside the folder firstDelfolder (path + "/" + templist[i]);//then delete the empty folderFlag =true; } } returnFlag; }}
Delete all files in the folder with Java