Here you can find the source of cleanDir(String directory)
Parameter | Description |
---|---|
directory | The directory to be cleaned |
public static boolean cleanDir(String directory) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**/* w w w . j a v a 2 s . c o m*/ * Empty a directory without deleting it * * @param directory The directory to be cleaned * @return true only if cleaning was successful */ public static boolean cleanDir(String directory) throws IOException { String[] files = ls(directory); boolean clean = true; // Some JVMs return null for empty dirs if (files != null) { for (String f : files) { String filename = directory + File.separator + f; File file = new File(filename); if (file.isDirectory()) clean = cleanDir(filename) && file.delete(); else clean = file.delete(); } } return clean; } /** * List the contents of a directory. */ public static String[] ls(String directory) throws IOException { if (!isDirectory(directory)) { throw new IOException("Invalid directory! " + directory); } return (new File(directory)).list(); } /** * Check if the argument is a directory. */ public static boolean isDirectory(String dir) { return (new File(dir)).isDirectory(); } }