Java Delete folder's tool class----very practical OH

Source: Internet
Author: User
Tags file separator
Import Java.io.File;
public class Deletefileutil {
/**
* Delete individual files
* @param the filename of the deleted file spath
* @return Single File Delete successfully returns TRUE, otherwise returns false
*/
public static Boolean DeleteFile (String spath) {
Boolean flag = false;
File File = new file (spath);
The path is a file and is not NULL to delete
if (File.isfile () && file.exists ()) {
File.delete ();
Flag = true;
}
return flag;
}
/**
* Delete directories (folders) and files under the directory
* @param spath The file path of the deleted directory
* @return Directory deletion successfully returns TRUE, otherwise returns false
*/
public static Boolean deletedirectory (String spath) {
If Spath does not end with a file delimiter, the file separator is automatically added
if (!spath.endswith (File.separator)) {
spath = spath + file.separator;
}
File Dirfile = new file (spath);
If the file for Dir does not exist, or if it is not a directory, exit
if (!dirfile.exists () | |!dirfile.isdirectory ()) {
return false;
}
Boolean flag = true;
Delete all files under a folder (including subdirectories)
file[] files = dirfile.listfiles ();
for (int i = 0; i < files.length; i++) {
Delete a child file
if (Files[i].isfile ()) {
Flag = DeleteFile (Files[i].getabsolutepath ());
if (!flag) break;
}//Delete subdirectories
else {
Flag = DeleteDirectory (Files[i].getabsolutepath ());
if (!flag) break;
}
}
if (!flag) return false;
Delete current directory
if (Dirfile.delete ()) {
return true;
} else {
return false;
}
}
/**
* Deletes the specified directory or file according to the path, whether it exists or not
* @param spath the directory or file to be deleted
* @return Delete succeeds returns True, otherwise returns false.
*/
public static Boolean DeleteFolder (String spath) {
Boolean flag = false;
File File = new file (spath);
To determine whether a directory or file exists
if (!file.exists ()) {//does not exist return false
return flag;
} else {
Decide whether to be a file
The delete file method is called if (File.isfile ()) {//is a file
return DeleteFile (spath);
The Delete directory method is called when the else {//is directory
return DeleteDirectory (spath);

}
}
}
}

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.