Here you can find the source of removeDirectory(Path directory)
Parameter | Description |
---|---|
directory | The directory to be removed. |
Parameter | Description |
---|---|
IOException | If there was an error deleting the file tree. In this case,some of the files may have already been deleted. |
public static void removeDirectory(Path directory) throws IOException
//package com.java2s; /*//from w w w. j av a 2 s. c o m * This file is part of the Raster Storage Archive (RSA). * * The RSA is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * The RSA is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * the RSA. If not, see <http://www.gnu.org/licenses/>. * * Copyright 2013 CRCSI - Cooperative Research Centre for Spatial Information * http://www.crcsi.com.au/ */ 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.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class Main { /** * Remove a directory and all of its contents. * * @param directory * The directory to be removed. * @throws IOException * If there was an error deleting the file tree. In this case, * some of the files may have already been deleted. */ public static void removeDirectory(Path directory) throws IOException { final Path canonicalPath = directory.toAbsolutePath().normalize(); if (canonicalPath.equals(canonicalPath.getRoot())) throw new IOException("Refusing to delete root directory."); Files.walkFileTree(canonicalPath, new SimpleFileVisitor<Path>() { /** * Delete files. */ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } /** * Delete directory after files have been removed (above). */ @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } } }); } /** * Remove a directory and all of its contents. * * @param directory * The directory to be removed. * @throws IOException * If there was an error deleting the file tree. In this case, * some of the files may have already been deleted. */ public static void removeDirectory(File directory) throws IOException { removeDirectory(directory.toPath()); } /** * Remove a directory and all of its contents. * * @param directory * The directory to be removed. * @throws IOException * If there was an error deleting the file tree. In this case, * some of the files may have already been deleted. */ public static void removeDirectory(String directory) throws IOException { removeDirectory(new File(directory)); } }