/** * Given a directory, removes all the content found in the directory. * @param dir The directory to be emptied * @throws Exception */ static public void emptyDirectory(File dir) throws Exception { String[] fileNames = dir.list(); if( null != fileNames ) { for(String fileName : fileNames){ File file = new File(dir,fileName); if( file.isDirectory() ) { emptyDirectory(file); } boolean deleted = false; try { deleted = file.delete(); } catch(Exception e) { throw new Exception("Unable to delete: "+file.getAbsolutePath(),e); } if( !deleted ){ throw new Exception("Unable to delete: "+file.getAbsolutePath()); } } } } }