Java Base IO Delete folder file

Source: Internet
Author: User

/**
* Define a method that can delete any folder, the folder path is entered by the keyboard
Note: Do not test under the C drive, please select the Useless folder test!
*/

1. Keyboard entry

  

Private StaticFile getfile () {//Keyboard EntryScanner sc =NewScanner (system.in); System.out.println ("Please enter the path of the folder:");  while(true){            //Infinite Loop until the input is up to the endString str =Sc.nextline (); //encapsulates a string into a file objectFile File =NewFile (str); //determine if the address entered is present            if(!file.exists ()) {System.out.println ("The folder you entered does not exist, please re-enter:"); //determine whether the input file}Else if(File.isfile ()) {System.out.println ("You entered the file path, please re-enter:"); //Match criteria return file}Else{                returnfile; }        }    }

2. Delete

    Private Static voidgetdelete (file file) {//generate file[] Array listfiles () method gets the folder file in the current directoryfile[] Files =File.listfiles (); //determines whether the empty//did you find the discussion basically the same        if(files!=NULL){            //Traverse             for(File file2:files) {//the file is deleted                if(File2.isfile ()) {file2.delete (); }Else if(File2.isdirectory ()) {//It 's a folder, recursive .Getdelete (file2); //empty folder Delete directlyFile2.delete (); }            }        }            }

Main method

 Public Static void Main (string[] args) {        = getfile ();            Getdelete (file);         // finally delete the directory folder         file.delete ();        System.out.println ("Delete succeeded!") );    }

Have any questions to leave a message to me. There is nothing bad in the place, please the vast number of Bo friends to give me advice.

Small program recently quite fire, I here have 120 small program source code, want to Bo friend, give me message I send address to you

Java Base IO Delete folder file

Related Article

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.