Java File Path Delete deleteQuietly(Object path)

Here you can find the source of deleteQuietly(Object path)

Description

delete Quietly

License

Apache License

Declaration

public static boolean deleteQuietly(Object path) 

Method Source Code

//package com.java2s;
/*//from  w  w  w .  j  a v a  2 s  .c  o  m
 * Copyright 2013 Primeton.
 *
 * 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.
 */

import java.io.File;
import java.io.FileFilter;

import java.util.ArrayList;

import java.util.List;

public class Main {

    public static boolean deleteQuietly(Object path) {
        return deleteQuietly(path, false);
    }

    public static boolean deleteQuietly(Object path, boolean isDeleteEmptyParent) {
        if (path == null) {
            return true;
        }
        File deleteFile = null;
        if (path instanceof String) {
            if (((String) path).trim().length() == 0) {
                return true;
            }
            deleteFile = new File((String) path);
        } else if (path instanceof File) {
            deleteFile = (File) path;
        } else {
            throw new IllegalArgumentException("file'" + path.getClass() + "' is not support type!");
        }

        try {
            File parent = deleteFile.getParentFile();
            boolean result = doDeleteQuietly(deleteFile);

            if (isDeleteEmptyParent) {
                boolean res = doDeleteEmptyParentQuietly(parent);
                if (result) {
                    result = res;
                }
            }
            return result;
        } catch (Throwable ignore) {
            return false;
        }
    }

    private static boolean doDeleteQuietly(File file) {
        try {
            boolean result = true;
            if (file.isDirectory()) {
                for (File aFile : file.listFiles()) {
                    boolean res = doDeleteQuietly(aFile);
                    if (result) {
                        result = res;
                    }
                }
            }
            file.delete();
            return result;
        } catch (Throwable ignore) {
            return false;
        }
    }

    private static boolean doDeleteEmptyParentQuietly(File parent) {
        try {
            if (parent == null) {
                return true;
            }
            boolean result = true;
            File parentFile = parent.getParentFile();
            if (parent.list().length == 0) {
                parent.delete();
                boolean res = doDeleteEmptyParentQuietly(parentFile);
                if (result) {
                    result = res;
                }
            }
            return result;
        } catch (Throwable ignore) {
            return false;
        }
    }

    public static List<File> listFiles(File dir, FileFilter filter) {
        if (dir == null) {
            throw new IllegalArgumentException("dir is null!");
        }
        if (!dir.exists()) {
            throw new IllegalArgumentException("Path'" + dir.getAbsolutePath() + "' is not existed!");
        }
        if (dir.isFile()) {
            throw new IllegalArgumentException("Path'" + dir.getAbsolutePath() + "' is file, not dir!");
        }
        List<File> fileList = new ArrayList<File>();
        doListFiles(dir, fileList, filter);
        return fileList;
    }

    private static void doListFiles(File dir, List<File> fileList, FileFilter filter) {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                doListFiles(file, fileList, filter);
            }
            if (filter != null && !filter.accept(file)) {
                continue;
            }
            fileList.add(file);
        }
    }
}

Related

  1. deleteParent(String filePath)
  2. deletePath(File dirPath)
  3. deletePath(File path)
  4. deletePath(final File path)
  5. deletePathRecursive(File path)
  6. deleteRecursive(File path)
  7. deleteRecursive(File path)
  8. deleteRecursive(File path)
  9. deleteRecursive(File path)