Here you can find the source of recursiveRemoveDir(File directory)
Parameter | Description |
---|---|
File directory | the directory that will be deleted. |
public static void recursiveRemoveDir(File directory) throws Exception
//package com.java2s; import java.io.*; public class Main { /**//from ww w. j a v a2s . c o m * This class del a directory recursively,that means delete all files and directorys. * * @param File directory the directory that will be deleted. */ public static void recursiveRemoveDir(File directory) throws Exception { if (!directory.exists()) throw new IOException(directory.toString() + " do not exist!"); String[] filelist = directory.list(); File tmpFile = null; for (int i = 0; i < filelist.length; i++) { tmpFile = new File(directory.getAbsolutePath(), filelist[i]); if (tmpFile.isDirectory()) { recursiveRemoveDir(tmpFile); } else if (tmpFile.isFile()) { try { tmpFile.delete(); } catch (Exception ex) { throw new Exception(tmpFile.toString() + " can not be deleted " + ex.getMessage()); } } } try { directory.delete(); } catch (Exception ex) { throw new Exception(directory.toString() + " can not be deleted " + ex.getMessage()); } finally { filelist = null; } } }