Java File Delete doDeleteQuietly(File file)

Here you can find the source of doDeleteQuietly(File file)

Description

do Delete Quietly

License

Apache License

Declaration

private static boolean doDeleteQuietly(File file) 

Method Source Code

//package com.java2s;
/*//  w  w  w .  j a  v  a  2 s  .  c  om
 * 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 {
    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;
        }
    }

    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. doDelete(File file)
  2. doDelete(File path)
  3. doDeleteEmptyDir(String dir)
  4. doDeleteEmptyParentQuietly(File parent)
  5. doDeleteFileOrDir(File fileOrDir)
  6. fileDelete(String fileName)
  7. fileDelete(String path)
  8. fileDelete(String path)