Java program to delete local files, java program implementation

Source: Internet
Author: User

Java program to delete local files, java program implementation

 

Import java. io. File;

Public class Test {
Public static void main (String args []) {
Test t = new Test ();
DelFolder ("d:/test ");
System. out. println ("OK ");
}

// Delete a folder
// Complete absolute path of the param folderPath folder

Public static void delFolder (String folderPath ){
Try {
DelAllFile (folderPath); // delete all contents
String filePath = folderPath;
FilePath = filePath. toString ();
Java. io. File myFilePath = new java. io. File (filePath );
MyFilePath. delete (); // delete an empty folder
} Catch (Exception e ){
E. printStackTrace ();
}
}

// Delete all objects in the specified folder
// Complete 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]); // delete the files in the folder first
DelFolder (path + "/" + tempList [I]); // Delete the empty folder.
Flag = true;
}
}
Return flag;
}
}

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.