Java File Delete nio deleteMatching(File baseFile, String regex)

Here you can find the source of deleteMatching(File baseFile, String regex)

Description

Destroys all files that match the given regex that are in the given directory.

License

Apache License

Declaration

public static void deleteMatching(File baseFile, String regex) 

Method Source Code


//package com.java2s;
/*//ww w .jav a  2s. c o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.*;

import java.nio.file.Files;

public class Main {
    /**
     * Destroys all files that match the given regex that
     * are in the given directory.
     * If a destroy fails it is ignored and an attempt is
     * made to destroy any other files that match.
     */
    public static void deleteMatching(File baseFile, String regex) {
        if (baseFile.exists() && baseFile.isDirectory()) {
            for (File child : listFiles(baseFile)) {
                if (child.getName().matches(regex)) {
                    try {
                        delete(child);
                    } catch (IOException ignore) {
                    }
                }
            }
        }
    }

    /**
     * Basically just like {@link File#listFiles()} but instead of returning null
     * returns an empty array. This fixes bug 43729
     */
    public static File[] listFiles(File dir) {
        File[] result = dir.listFiles();
        if (result == null) {
            result = new File[0];
        }
        return result;
    }

    /**
     * Basically just like {@link File#listFiles(FilenameFilter)} but instead of returning null
     * returns an empty array. This fixes bug 43729
     */
    public static File[] listFiles(File dir, FilenameFilter filter) {
        File[] result = dir.listFiles(filter);
        if (result == null) {
            result = new File[0];
        }
        return result;
    }

    /**
     * Recursively delete a file or directory.
     * 
     * @throws IOException
     *           if the file or directory couldn't be deleted. Unlike File.delete,
     *           which just returns false.
     */
    public static void delete(File file) throws IOException {
        if (!file.exists())
            return;

        if (file.isDirectory()) {
            for (File child : listFiles(file)) {
                delete(child);
            }
        }

        Files.delete(file.toPath());
    }

    /**
     * Recursively delete a file or directory.
     * A description of any files or directories
     * that can not be deleted will be added to failures
     * if failures is non-null.
     * This method tries to delete as much as possible.
     */
    public static void delete(File file, StringBuilder failures) {
        if (!file.exists())
            return;

        if (file.isDirectory()) {
            for (File child : listFiles(file)) {
                delete(child, failures);
            }
        }

        try {
            Files.delete(file.toPath());
        } catch (IOException e) {
            if (failures != null) {
                failures.append("Could not delete ").append(file).append(" due to ").append(e.getMessage())
                        .append('\n');
            }
        }
    }
}

Related

  1. deleteFileRescursive(File file)
  2. deleteFiles(File directory, String affix)
  3. deleteIfEmpty(final File directory)
  4. deleteLocalFile(final String fileName)
  5. deleteLocalFileOrDirectory(File file)
  6. deletePidFile()
  7. deleteQuietly(File file)
  8. deleteRecursive(File fileOrDirectory)
  9. deleteRecursively(File fileOrDir)