Java File Path Delete deleteDir(final String path)

Here you can find the source of deleteDir(final String path)

Description

delete Dir

License

Open Source License

Declaration

public static void deleteDir(final String path) throws IOException 

Method Source Code

//package com.java2s;
/**/* w  w w  . j a  va2s . c om*/
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */

import java.io.File;

import java.io.IOException;

public class Main {
    public static void deleteDir(final String path) throws IOException {
        File file = new File(path);
        deleteDir(file);
    }

    public static void deleteDir(File dir) throws IOException {
        if (dir.isFile()) {
            throw new IOException("IOException -> BadInputException: not a directory.");
        }
        File[] files = dir.listFiles();
        if (files == null) {
            dir.delete();
            return;
        }
        for (File file : files) {
            if (file.isFile()) {
                file.delete();
            } else {
                deleteDir(file);
            }
        }
    }

    public static void delete(final String path) throws IOException {
        File file = new File(path);
        delete(file);
    }

    public static void delete(final File file) throws IOException {
        if (!file.exists()) {
            return;
        }
        if (file.isFile()) {
            deleteFile(file);
        } else {
            deleteDir(file);
        }
    }

    public static void deleteFile(final String path) throws IOException {
        File file = new File(path);
        deleteFile(file);
    }

    public static void deleteFile(File file) throws IOException {

        if (file.isDirectory()) {
            throw new IOException("IOException -> BadInputException: not a file.");
        }

        if (file.exists() == false) {
            return;
        }

        if (file.delete() == false) {
            throw new IOException("Cannot delete file. filename = " + file.getPath());
        }
    }
}

Related

  1. deleteDir(File file)
  2. deleteDir(File path)
  3. deleteDir(File path)
  4. deleteDir(File path)
  5. deleteDir(File path)
  6. deleteDir(String path)
  7. deleteDir(String path)
  8. deleteDirByDoc(String path)
  9. deleteDirectory(File directoryPath)