/**
* Define a method , Can delete any folder , The folder path is entered by the keyboard
Be careful : Not in C Test under the disk , Please select a useless folder to test !
*/

1. Keyboard Entry

private static File getfile() {
// Keyboard Entry
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the path to the folder :");
while(true){
// Infinite loop Until the input is correct
String str = sc.nextLine();
// Encapsulate a string into file object
File file = new File(str);
// Determine whether the input address exists
if(!file.exists()){
System.out.println(" The folder you entered does not exist , Please re-enter :");
// Determine whether the input is a file
}else if(file.isFile()){
System.out.println(" You enter the file path , Please re-enter :");
// eligible return file
}else{
return file;
}
}
}

2. Delete

 private static void getDelete(File file) {
// Generate File[] Array listFiles() Method to get the folder in the current directory file
File[] files = file.listFiles();
// Determine whether it is null // Did you find that the discussion is basically the same
if(files!=null){
// Traverse
for (File file2 : files) {
// If it's a file, delete it
if(file2.isFile()){
file2.delete();
}else if(file2.isDirectory()){
// If it's a folder, it's recursive
getDelete(file2);
// Delete empty folder directly
file2.delete();
}
}
} }

main Method

public static void main(String[] args) {
File file = getfile();
getDelete(file);
// Finally, delete the directory folder
file.delete();
System.out.println(" Delete successful !");
}

Leave me a message if you have any questions . There's something wrong with it , Please give me more advice .

Wechat apps are very popular recently , I have 120 A wechat applet source code , My friends , Leave me a message I'll send you the address

java Basics IO Delete more articles about folder files

  1. java Implement keyword based folder ( file ) Search for 、 Folder ( file ) Copy 、 Delete

    I've been working on a project recently , We need to implement these functions , I checked a lot of information on the Internet , I studied for several days and finally realized , Now share with you . One .JAVA Achieve folder search   Search on baidu N A technical article , From the materials shared by the big bulls, I finally wrote ...

  2. Java Basics -IO Stream object random access file (RandomAccessFile)

    Java Basics -IO Stream object random access file (RandomAccessFile) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . One .RandomAccessFile brief introduction Examples of this class support ...

  3. JavaSE File recursive deletion & Get all the files in the folder to .jpg The absolute path of the file

    1. Recursively delete files Suppose there are subfolders below a folder , If you delete it, it will Report errors , At this time, we need to use recursion to delete all the files and folders in this file folder package cn.itcast.digui; impor ...

  4. java Basics IO Flow synthesis plus exercises

    IO Stream beginners are a little confused in their study , Today we will talk about IO flow , I hope the explanation can help you IO Streams are divided into byte streams , Character stream , Buffer flow . We just need to remember these three . 1* The byte stream has : Byte input stream (FileInputStrea ...

  5. Java Basics -IO Flow object of File class

    Java Basics -IO Flow object of File class author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . One .IO Technology overview Think back to the program you wrote before , The data is all in memory , Once the program is finished , There's no data , Wait a minute ...

  6. Java Basics -IO Byte stream of stream object (Stream)

    Java Basics -IO Byte stream of stream object (Stream) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . In the notes I shared earlier , We're always working on files or folders , No data was written to the file . present ...

  7. Java Basics IO flow ( Two ) Byte stream case

    JAVA Basics IO flow ( One )https://www.cnblogs.com/deepSleeping/p/9693601.html ① Read the contents of the specified file , according to 16 Output to the console among ,Integer.to ...

  8. Java Basics -IO Compressed stream of stream objects (ZipOutputStream) And decompress the stream (ZipInputStream)

    Java Basics -IO Compressed stream of stream objects (ZipOutputStream) And decompress the stream (ZipInputStream) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . I've shared a lot before J ...

  9. Java Basics -IO Print stream of stream object (PrintStream And PrintWriter)

    Java Basics -IO Print stream of stream object (PrintStream And PrintWriter) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . One . Print stream features There are two print objects , The byte print stream (P ...

Random recommendation

  1. Android database LiteOrm Use

    1. What is? LiteOrm LiteOrm It's a compact . Powerful . Faster than the system's own database operation performance 1 Times android ORM Framework class library , Developers a line of code to achieve the database to add, delete, change and query operations , And the persistence and automatic mapping of entity relations . ...

  2. Inquire about DDOS attack IP list grep -i

    [root@xsx ~]# netstat -an|grep -i syn_recv|awk '{print $5}'|awk -F\: '{print $1}'|sort|uniq -c 1 149 ...

  3. Android The way to judge whether a user is a tablet or a mobile phone

    public boolean isTabletDevice() {        TelephonyManager telephony = (TelephonyManager) mContext.ge ...

  4. xml Introduction -- Two days to learn xml

    Preface A long time ago , I have seen 1000+ page xml book , There are also n The concept of many ,XSL,Xquery, It's a headache . But I need to use it recently , So in w3c Make up for it . The following is a brief summary of the related concepts , But yes. XSL And so on ...

  5. Elon Musk: Technology maniacs who subvert several industries at the same time

    In Apple's famous " I don't think so " In advertising , Praise the crazy guys who changed the world ." They don't like to be conventional , I don't want to be content with the status quo "," They changed things "," they ...

  6. Power Pivot Table properties cannot be switched back to table preview mode

    In the near future Office365 After the user upgrade, the problem of Power Pivot The problem of inputting Chinese in , But at the same time, it also brings a new problem, that is, the default table property window is “ Query editor ” Pattern , And can't switch back to “ Table Preview ” Pattern . This article shares with you in this case ...

  7. Lucene Chapter one 【 Introduce Lucene、 Quick start 】

    What is? Lucene?? Lucene yes apache Software Foundation released an open source full text search engine toolkit , By senior full text retrieval experts Doug Cutting written , It's the architecture of a full-text search engine , Provides complete index creation ...

  8. hdu_1027( ok ... cattle ...next_permutation You can also water it )

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; int ...

  9. matlab Read cvs There are several ways to file

    matlab Read CVS There are several ways to file : 1, practical csvread() function   csvread() There are three ways to use functions : 1.M = csvread('filename')2.M = csvread('fi ...

  10. Spring Security Dynamic configuration of resource permissions

    stay Spring Security Through the database dynamic configuration url Resource permissions , You need to configure a validation filter to load resource permissions . verification . When the system starts , Load the system resource permission list into the database , When there is a request for access , By comparing the system resource permissions ...