/**
* 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