递归删除指定目录下所有文件及子目录

public class Main 
{
	
	    public static void main(String[] args)
	    {
	    	Main m = new Main();
	    	m.deleteFile(new File("e:\\test\\"));
	    }
	    
	    public void deleteFile(File file)
	    {
	    		File[] temp = file.listFiles();
	    		for(int i=0;i<temp.length;i++)
	    		{
	    			System.out.println(temp[i].getName());
	    			if(temp[i].isDirectory())
	    	    	{
	    				if(temp[i].listFiles().length!=0)
	    					this.deleteFile(temp[i]);
	    				this.deleteDir(temp[i]);
	    	    	}else
	    	    	{
	    	    		temp[i].delete();
	    	    	}
	    		}
	    }
	
	    private void deleteDir(File file)
	    {
	    	if(file.listFiles().length==0)
	    		file.getAbsoluteFile().delete();
	    }
}
 

你可能感兴趣的:(linux,windows,OS,XP,groovy)