Java Path Delete nio deleteAllFilesRecursively(Path path)

Here you can find the source of deleteAllFilesRecursively(Path path)

Description

delete All Files Recursively

License

Apache License

Declaration

public static int deleteAllFilesRecursively(Path path) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w ww.  j  a  v a  2 s  .  c  om*/
 * Copyright (C) 2016 Google Inc.
 *
 * 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.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    private static final PathMatcher ALL_FILES = new PathMatcher() {
        @Override
        public boolean matches(Path path) {
            return true;
        }

        @Override
        public String toString() {
            return "**";
        }
    };

    public static int deleteAllFilesRecursively(Path path) throws IOException {
        return deleteFilesRecursively(path, ALL_FILES);
    }

    /**
     * Deletes the files that match the PathMatcher.
     *
     * <p> Note that this method doesn't delete the directories, only the files inside those
     * directories. This is fine for our use case since the majority of SCMs don't care about empty
     * directories.
     *
     * @throws IOException If it fails traversing or deleting the tree.
     */
    public static int deleteFilesRecursively(Path path, final PathMatcher pathMatcher) throws IOException {
        final AtomicInteger counter = new AtomicInteger();
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(file)) {
                    Files.delete(file);
                    counter.incrementAndGet();
                }
                return FileVisitResult.CONTINUE;
            }
        });
        return counter.get();
    }
}

Related

  1. delete(Path root)
  2. delete(Path targetPath)
  3. delete(Path... paths)
  4. delete(String filePath, String fileName)
  5. delete(String targetFilePath)
  6. deleteContent(Path directory)
  7. deleteEmptyDirsUpTo(Path from, Path to)
  8. deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)
  9. deleteFile(Path p)