Java Path Delete nio forceDelete(Path path)

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

Description

Forces the deletion of the file or directory corresponding to the specified path.

License

Apache License

Parameter

Parameter Description
path the path of the file or directory to delete.

Exception

Parameter Description
IOException if a problem occurs while deleting the file or directory.

Declaration

public static void forceDelete(Path path) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w ww  .  java2  s  . c om*/
 * Copyright 2013 Benjamin Lerer
 * 
 * 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.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    /**
     * <code>FileVisitor</code> used to delete directory content.
     */
    private static SimpleFileVisitor<Path> DELETER = new SimpleFileVisitor<Path>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            file.toFile().delete();

            return FileVisitResult.CONTINUE;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {

            dir.toFile().delete();

            return FileVisitResult.CONTINUE;
        }
    };

    /**
     * Forces the deletion of the file or directory corresponding to the specified path.
     * 
     * @param path the path of the file or directory to delete.
     * @throws IOException if a problem occurs while deleting the file or directory.
     */
    public static void forceDelete(Path path) throws IOException {
        Files.walkFileTree(path, DELETER);
    }
}

Related

  1. deleteRecursively(Path directory)
  2. deleteRecursively(Path path)
  3. deleteTmpDir(Path path)
  4. deleteTreeBelowPath(Path startHerePath)
  5. doDelete(@Nonnull Path path)
  6. optimisticDelete(Path path)
  7. readAndDelete(final Path p)
  8. recursiveDelete(Path directory)
  9. recursiveDelete(Path path)