Java deletes files and folders

Source: Internet
Author: User

/**
* Delete a single object
* The path of the tempfilepath file is: "d :\\ projectpath \ attach \ HTML \ test.html"
* @ Param Image
* @ Return
*/
Public void deletetempfile (string tempfilepath ){
File file = new file (tempfilepath );

String statenotexsit = tempfilepath + "the file does not exist. deletion failed! ";
String stateerror = tempfilepath + "the file is in use. deletion failed! ";
String statesuccess = tempfilepath + "deleted successfully! ";
If (file. exists ()){
Boolean d = file. Delete ();

If (d ){
System. Out. println (statesuccess );
} Else {
System. Out. println (stateerror );
}
} Else {
System. Out. println (statenotexsit );
}
}

/**
* Delete folders and all contents in the folder
* Dir = new file ("D: \ projectpath \ attach \ html ");
* @ Param Image
* @ Return
*/
Public Boolean deletefolder (File DIR ){
File filelist [] = dir. listfiles ();
Int listlen = filelist. length;
For (INT I = 0; I <listlen; I ++ ){
If (filelist [I]. isdirectory ()){
Deletefolder (filelist [I]);
} Else {
If (! Filelist [I]. Delete ())
Return false;
}
}
If (! Dir. Delete ())
Return false; // Delete the current directory
Else
Return true;
}

/**
* Batch Delete objects
* Tempfilepath is the set of file locations
* @ Return
*/
Public void deletealltemppdffile (arraylist <string> tempfilepath ){
For (INT I = 0; I <tempfilepath. Size (); I ++ ){
File file = new file (tempfilepath. Get (I ));

String statenotexsit = tempfilepath + "the file does not exist. deletion failed! ";
String stateerror = tempfilepath + "the file is in use. deletion failed! ";
String statesuccess = tempfilepath + "deleted successfully! ";
If (file. exists ()){
Boolean d = file. Delete ();

If (d ){
System. Out. println (statesuccess );
} Else {
System. Out. println (stateerror );
}
} Else {
System. Out. println (statenotexsit );
}
}
}

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.