Java Io delete folders

Source: Internet
Author: User
Deleting a folder includes the following situations: files in the folder, subfolders (with files) in the folder, and subfolders (empty folders) in the folder ). Therefore, the program should be written to address these three situations. Import Java. io. file; public class test {public static void main (string ARGs []) {test T = new test (); delfolder ("C:/BB"); system. out. println ("deleted");} // delete a folder // complete absolute path of the param folderpath folder public static void delfolder (string folderpath) {try {delallfile (folderpath ); // After deleting all content, string filepath = folderpath; filepath = filepath. tostring (); Java. io. file myfilepath = new Java. io. file (filepath); Myf Ilepath. delete (); // delete an empty folder} catch (exception e) {e. printstacktrace () ;}}// delete all files in the specified folder // The full absolute path of the param path folder public static Boolean delallfile (string path) {Boolean flag = false; file file = new file (PATH); If (! File. exists () {return flag;} If (! File. isdirectory () {return flag;} string [] templist = file. list (); file temp = NULL; For (INT I = 0; I <templist. length; I ++) {If (path. endswith (file. separator) {temp = new file (path + templist [I]);} else {temp = new file (path + file. separator + templist [I]);} If (temp. isfile () {temp. delete ();} If (temp. isdirectory () {delallfile (path + "/" + templist [I]); // first delete the file delfolder (path + "/" + templist [I]) in the folder; // then delete the empty folder flag = true;} return flag ;}}

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.