Java deletes all content in a folder without deleting instances of the folder itself _java

Source: Internet
Author: User

The

instance is as follows:

Package com.xx;

Import Java.io.File;
	  public class Test {public static void main (string[] args) {String fileroot = "c:/users/xx/desktop/xx/xxx";
      Delfolder (Fileroot);
	System.out.println ("deleted"); /////Delete folder////param FolderPath folder full absolute path public static void Delfolder (String folderpath) {try {Delall File (FolderPath);
Delete all contents inside//Do not want to delete the best clip hidden below//String FilePath = FolderPath;
FilePath = Filepath.tostring ();
Java.io.File Myfilepath = new Java.io.File (FilePath); Myfilepath.delete ();
		Delete Empty folders} catch (Exception e) {e.printstacktrace ();
		}///delete all files//param path folder full absolute path public static Boolean delallfile (String path) {Boolean flag = False in the specified folder;
		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 file in Folder//Delfolder (path + "/" + Templi
			St[i]);/delete empty folder flag = true;
	} return flag;
 }
}

The above Java Delete all the contents of the folder without deleting the folder itself is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.