Java Delete file picture and folder directory and file

Source: Internet
Author: User
Tags file separator

/**
* Delete individual files
* @param filename of the file to be deleted
* @return Single File Delete successfully returns TRUE, otherwise returns false
*/
public static Boolean DeleteFile (string filename) {
File File = new file (filename);
If a 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 picture

Package com.data.maint;
Import Java.io.file;
Import Org.apache.log4j.logger;
public class Cleartempimg {
Private static Final Logger logger = Logger.getlogger (Cleartempimg.class);
Private final static string fs = System.getproperty ("File.separator");
private static string tomcat_home = System.getproperty ("Catalina.home");
private static string path = "Temp";
private static final String Temp_path = tomcat_home + FS + path + FS;
private static Boolean isrunning = false;
public void Run () {
if (!isrunning) {
Logger.debug ("Delete expired picture in/www.111cn.net/temp/directory execution start ...");
IsRunning = true;
Logger.debug ("Start the Delete Tomcat Temp folder png file task");
Logger.debug ("The path to delete picture file is" + Temp_path);
File Filetemp = new file (Temp_path);
To determine whether a file exists
Boolean falg = false;
Falg = Filetemp.exists ();
if (FALG) {
Logger.debug ("Temp file exists");
if (true = = Filetemp.isdirectory ()) {
Logger.debug ("Temp file is a directory");
string[] png = Filetemp.list ();
for (int i = 0; i < png.length; i++) {
if (true = = Png[i].endswith ("png")) {
File File = new file (Temp_path + FS + png[i]);
if (True==file.isfile ()) {
Boolean flag = false;
Flag = File.delete ();
if (flag) {
Logger.debug ("Delete invalid picture file successfully:" + file.getname ());
}
}
}
}
}
} else {
Logger.debug ("No Tomcat/temp folder found, failed to execute, please manually delete all PNG files under temp");
}
IsRunning = false;
Logger.debug ("Delete expired picture in/www.111cn.net/temp/directory execution end ...");
}
}
}


A little more complicated.

/**
* Delete the directory and the files in the directory
* @param dir The file path of the directory to be deleted
* @return Directory deletion successfully returns TRUE, otherwise returns false
*/
public static Boolean DeleteDirectory (String dir) {
If Dir does not end with a file separator, the file separator is automatically added
if (!dir.endswith (File.separator))
dir = dir + file.separator;
File Dirfile = new file (dir);
If the file for Dir does not exist, or if it is not a directory, exit
if ((!dirfile.exists ()) | | (!dirfile.isdirectory ())) {
System.out.println ("Delete directory failed: + 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 a child file
if (Files[i].isfile ()) {
Flag = Deletefileutil.deletefile (Files[i].getabsolutepath ());
if (!flag)
Break
}
Deleting 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/www.111cn.net/test1/test2/testfile.txt";
Deletefileutil.deletefile (file);
System.out.println ();
Delete a directory
String dir = "C:/mb.111cn.net/test0/test1";
Deletefileutil.deletedirectory (dir);
System.out.println ();
deleting files
dir = "C:/test/test0";
Deletefileutil.delete (dir);

}

}

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.