Java Directory Clear clearDirectory(File dir)

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

Description

clear Directory

License

Open Source License

Declaration

public static void clearDirectory(File dir) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w w  .  j  a v a2s  .  c  o m
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

import java.io.File;
import java.io.IOException;

public class Main {
    public static void clearDirectory(File dir) throws IOException {
        if (!dir.isDirectory()) {
            return;
        }

        for (File subFile : dir.listFiles()) {
            if (subFile.isFile()) {
                if (!subFile.delete()) {
                    throw new IOException("Can not delete \"" + subFile.getAbsolutePath() + "\".");
                }
            } else if (subFile.isDirectory()) {
                removeDirectory(subFile);
            }
        }
    }

    public static void removeDirectory(File dir) throws IOException {
        if (!dir.isDirectory()) {
            return;
        }

        clearDirectory(dir);
        if (!dir.delete()) {
            throw new IOException("Can not delete \"" + dir.getAbsolutePath() + "\".");
        }
    }
}

Related

  1. cleanDirectory(IProgressMonitor monitor, File directory)
  2. cleanDirectory(IProgressMonitor monitor, File directory, File base, int step)
  3. cleanDirectory(String dir)
  4. cleanDirectory(String dirPath)
  5. clearDirctory(File dir)
  6. clearDirectory(File dir)
  7. clearDirectory(File dir, boolean doTree)
  8. clearDirectory(File f)
  9. clearDirectory(File f)