Delete files in Java, delete directories and all files under directory

Source: Internet
Author: User

Translated from: http://www.tqcto.com/article/recommend/142.html

Function: Delete all subdirectories and files in a directory and directory

knowledge point: File.delete () used to remove "A file or empty directory" ! So to delete a directory and all the files and subdirectories in it, recursively delete the code example as follows:

 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 ();   &Nbsp; }           main (String[] args)  {         dodeleteemptydir ("New_dir1");         String newDir2 =  "New_dir2";          success = deletedir ( file (NEWDIR2));           (Success)  {             System.out.println ("successfully deleted populated directory: " &NBSP;+&NBSP;NEWDIR2);         }  {             system.out.println ("failed to delete populated directory: " &NBSP;+&NBSP;NEWDIR2);        }          }}

From the original: programming technology

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Delete files in Java, delete directories and all files under directory

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.