Here you can find the source of cleanDirectory(File directory)
Parameter | Description |
---|---|
directory | a parameter |
public static void cleanDirectory(File directory) throws IOException
//package com.java2s; import java.io.File; import java.io.IOException; public class Main { /**/*from ww w . j av a 2 s .com*/ * Deletes a directory (including any contents) and recreates it so that it * is empty * * @param directory * @return */ public static void cleanDirectory(File directory) throws IOException { deleteDirectory(directory); if (!directory.mkdirs()) throw new IOException("Directory could not be re-created during clean: " + directory.getAbsolutePath()); } /** * Recursively deletes the contents of a directory, and the directory itself * * @param directory * @return */ public static boolean deleteDirectory(File directory) { boolean success = true; if (directory.exists()) { // delete contents File[] files = directory.listFiles(); for (File f : files) { if (f.isDirectory()) { success = success && deleteDirectory(f); } else { success = success && f.delete(); } } // delete directory itself success = success && directory.delete(); } return success; } }