Java Delete Directory deleteDirectory(final File dir)

Here you can find the source of deleteDirectory(final File dir)

Description

delete Directory

License

Apache License

Declaration

final static void deleteDirectory(final File dir) throws Exception 

Method Source Code


//package com.java2s;
/*/* w w w. j  a  v  a  2 s  .  c om*/
 * #%L
 * xcode-maven-plugin
 * %%
 * Copyright (C) 2012 SAP AG
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class Main {
    final static void deleteDirectory(final File dir) throws Exception {

        //
        // FileUtils.deleteDirectory does not work with symbolic links
        //

        if (!dir.exists())
            return;

        if (dir.isFile())
            throw new IllegalStateException(dir + " is not a directory.");

        process("rm", "-rf", dir.getAbsolutePath());

        if (dir.exists()) {
            throw new IllegalStateException(
                    "The removal of the dir " + dir.getAbsolutePath() + " failed. The dir still exists.");
        }
    }

    private static void process(final String... command) throws IOException, InterruptedException {

        Process process = new ProcessBuilder(command).start();

        if (process.waitFor() != 0)
            throw new IllegalStateException("Cannot execute '" + Arrays.asList(command) + "'.");
    }
}

Related

  1. deleteDirectory(File root)
  2. deleteDirectory(File root)
  3. deleteDirectory(File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself)
  4. deleteDirectory(final File dir)
  5. deleteDirectory(final File dir)
  6. deleteDirectory(final File directory)
  7. deleteDirectory(final File directory)
  8. deleteDirectory(final File directory)
  9. deleteDirectory(final String dir)