Java deletes all files under the specified path and all folders in the folder, excluding the folder

Source: Internet
Author: User
package file;

Import Java.io.File;

Deletes all files in the specified path, and all folders in the folder, excluding the folder public
class Filedel {public	
	static void Delallfiles (file file,string Singal) {
		if (!file.exists ()) {
			System.out.println ("The path is not present:" +file);
			return;
		}
		if (singal==null| | "". Equals (Singal)) {
			singal= "-";
		}			
		SYSTEM.OUT.PRINTLN (singal+) directory ["+file.getname () +"]: ");
		singal=singal+ "-";
		File[] Files=file.listfiles ();
			if (files.length>0) {	
				for (File f:files) {
					if (f.isdirectory ()) {//If it is a directory
						delallfiles (f, singal);	Recursive
						System.out.println (singal+ "directory [" +f.getname () + "] deleted");	
						F.delete ();		Delete this folder
					}
					else{
						System.out.println (singal+ file "+f.getname () +" "deleted");	
						F.delete ();
					} else
				}//for
			}//if
			else{
				System.out.println ("* * * * * * * * No files in this directory * * * *)";
			}		
	Static
	@SuppressWarnings ("static-access") public
	static void Main (string[] args) {
		Filedel tt=new Filedel ();
		Tt.delallfiles (New File ("G:\\ABC"), null);
	}

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.