Here you can find the source of forceDelete(Path path)
Parameter | Description |
---|---|
path | the path of the file or directory to delete. |
Parameter | Description |
---|---|
IOException | if a problem occurs while deleting the file or directory. |
public static void forceDelete(Path path) throws IOException
//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); } }