java删除文件

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();
	    }
}

你可能感兴趣的:(java)