Here you can find the source of deletePathRecursively(String path)
Parameter | Description |
---|---|
path | pathname to be deleted |
Parameter | Description |
---|---|
IOException | when fails to delete |
public static void deletePathRecursively(String path) throws IOException
//package com.java2s; /*/*ww w . j a v a 2 s. c o m*/ * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 * (the ?License??). You may not use this work except in compliance with the License, which is * available at www.apache.org/licenses/LICENSE-2.0 * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied, as more fully set forth in the License. * * See the NOTICE file distributed with this work for information regarding copyright ownership. */ import java.io.File; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class Main { /** * Deletes a path recursively. * * @param path pathname to be deleted * @throws IOException when fails to delete */ public static void deletePathRecursively(String path) throws IOException { Path root = Paths.get(path); Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { throw e; } } }); } /** * Deletes the file or directory. * * Current implementation uses {@link java.io.File#delete()}, may change if there is a better * solution. * * @param path pathname string of file or directory * @throws IOException when fails to delete */ public static void delete(String path) throws IOException { File file = new File(path); if (!file.delete()) { throw new IOException("Failed to delete " + path); } } }