Java File Path Delete deleteDirectory(File path)

Here you can find the source of deleteDirectory(File path)

Description

delete Directory

License

Open Source License

Declaration

static public boolean deleteDirectory(File path) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2005-2008 Whirlwind Match Limited. All rights reserved.
 *
 * This is open source software; you can use, redistribute and/or modify
 * it under the terms of the Open Software Licence v 3.0 as published by the 
 * Open Source Initiative.//from   w w  w .j  av a2  s. c  o  m
 *
 * You should have received a copy of the Open Software Licence along with this
 * application. if not, contact the Open Source Initiative (www.opensource.org)
 *****************************************************************************/

import java.io.File;

import java.util.logging.Logger;

public class Main {
    static private Logger log;

    static public boolean deleteDirectory(File path) {
        boolean deleted = deleteDirectoryInternal(path);
        log.info(deleted ? "Delete succeeded for path: " + path : "Delete failed for path: " + path);
        return deleted;
    }

    static private boolean deleteDirectoryInternal(File path) {
        if (!path.exists()) {
            return true;
        }

        File[] files = path.listFiles();
        boolean succeeded = true;
        for (File file : files) {
            if (file.isDirectory()) {
                succeeded &= deleteDirectoryInternal(file);
            } else {
                succeeded &= file.delete();
            }
        }
        return succeeded && path.delete(); // Deliberately won't call path.delete() if !succeeded (it'll fail)
    }
}

Related

  1. deleteDirectory(File path)
  2. deleteDirectory(File path)
  3. deleteDirectory(File path)
  4. deleteDirectory(File path)
  5. deleteDirectory(File path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path)
  8. deleteDirectory(File path, boolean deleteMyself, boolean deleteHidden)
  9. deleteDirectory(File path, boolean includeDir)