Delete all files in the folder with Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.