Java code Complete Delete file, folder operation

Source: Internet
Author: User

Import Java.io.File;

/**
* Delete files and directories
*
* @author Chen
*
*/
public class Deletefileutil {

/**
* Delete files, can be files or folders
*
* @param fileName
* The file name to delete
* @return Delete succeeds returns true, otherwise false
*/
public static Boolean Delete (String fileName) {
File File = new file (fileName);
if (!file.exists ()) {
System.out.println ("Delete file failed:" + fileName + "does not exist!") ");
return false;
} else {
if (File.isfile ())
return DeleteFile (FileName);
Else
return DeleteDirectory (FileName);
}
}

/**
* Delete individual files
*
* @param fileName
* filename of the file to be deleted
* @return Single File Delete successfully returns TRUE, otherwise false
*/
public static Boolean DeleteFile (String fileName) {
File File = new file (fileName);
If the file path corresponds to a file that exists and is a file, it is deleted directly
if (file.exists () && file.isfile ()) {
if (File.delete ()) {
System.out.println ("Delete single file" + FileName + "Success!") ");
return true;
} else {
System.out.println ("Delete single file" + FileName + "Failed!") ");
return false;
}
} else {
System.out.println ("Delete single file failed:" + fileName + "does not exist!") ");
return false;
}
}

/**
* Delete files in directories and directories
*
* @param dir
* The file path of the directory to be deleted
* @return Directory Delete successfully returns TRUE, otherwise false
*/
public static Boolean deletedirectory (String dir) {
If Dir does not end with a file delimiter, the file delimiter is automatically added
if (!dir.endswith (File.separator))
dir = dir + file.separator;
File Dirfile = new file (dir);
If the dir corresponding file does not exist, or is not a directory, exit
if ((!dirfile.exists ()) | | (!dirfile.isdirectory ())) {
System.out.println ("Failed to delete directory:" + dir + "does not exist!) ");
return false;
}
Boolean flag = true;
Delete all files in a folder including subdirectories
file[] files = dirfile.listfiles ();
for (int i = 0; i < files.length; i++) {
Delete sub-Files
if (Files[i].isfile ()) {
Flag = Deletefileutil.deletefile (Files[i].getabsolutepath ());
if (!flag)
Break
}
Delete subdirectories
else if (files[i].isdirectory ()) {
Flag = Deletefileutil.deletedirectory (Files[i]
. GetAbsolutePath ());
if (!flag)
Break
}
}
if (!flag) {
System.out.println ("Delete directory failed! ");
return false;
}
Delete current directory
if (Dirfile.delete ()) {
System.out.println ("delete directory" + dir + "Success!") ");
return true;
} else {
return false;
}
}

public static void Main (string[] args) {
Delete a single file
String file = "C:/test/test.txt";
Deletefileutil.deletefile (file);
System.out.println ();
Delete a directory
String dir = "C:/test";
Deletefileutil.deletedirectory (dir);
System.out.println ();
deleting files
dir = "C:/test/test0";
Deletefileutil.delete (dir);

}

}

Copy from: http://blog.csdn.net/songylwq/article/details/6106976

Java code Complete Delete file, folder operation

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.