Java Directory Clean cleanDir(File dir)

Here you can find the source of cleanDir(File dir)

Description

Delete everything inside a directory.

License

Open Source License

Parameter

Parameter Description
dir the directory

Exception

Parameter Description
InterruptedException on interruption

Declaration

public static void cleanDir(File dir) throws InterruptedException 

Method Source Code


//package com.java2s;
/*// w  w  w  . j a v  a2 s.  com
 * SK's Minecraft Launcher
 * Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.*;

public class Main {
    /**
     * Delete everything inside a directory.
     * 
     * @param dir the directory
     * @throws InterruptedException on interruption
     */
    public static void cleanDir(File dir) throws InterruptedException {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        if (!dir.exists()) {
            return;
        }

        for (File f : dir.listFiles()) {
            if (f.isDirectory()) {
                cleanDir(f);
                f.delete();
            } else {
                f.delete();
            }
        }
    }
}

Related

  1. cleanDir(File dir)
  2. cleanDir(File directory)
  3. cleanDir(final File dir)
  4. cleanDir(final File dir)