一个不小心递归产生了超深度目录,右键“删除”无法,直接删除失败,命令删除提示拒绝访问;360强力删除不行,无助之际,使用Java编程的方式完成了删除,直接上代码,如下:
try {
org.apache.commons.io.FileUtils.deleteDirectory(new File("要删除的磁盘目录绝对路径"));
}catch (IOException e){
throw e;
}
org.apache.commons.io.FileUtils#deleteDirectory
/**
* Deletes a directory recursively.
*
* @param directory directory to delete
* @throws IOException in case deletion is unsuccessful
* @throws IllegalArgumentException if {@code directory} is not a directory
*/
public static void deleteDirectory(final File directory) throws IOException {
Objects.requireNonNull(directory, "directory");
if (!directory.exists()) {
return;
}
if (!isSymlink(directory)) {
cleanDirectory(directory);
}
delete(directory);
}
org.apache.commons.io.FileUtils#cleanDirectory
/**
* Cleans a directory without deleting it.
*
* @param directory directory to clean
* @throws NullPointerException if the given {@code File} is {@code null}.
* @throws IllegalArgumentException if directory does not exist or is not a directory.
* @throws IOException if an I/O error occurs.
* @see #forceDelete(File)
*/
public static void cleanDirectory(final File directory) throws IOException {
final File[] files = listFiles(directory, null);
final List<Exception> causeList = new ArrayList<>();
for (final File file : files) {
try {
forceDelete(file);
} catch (final IOException ioe) {
causeList.add(ioe);
}
}
if (!causeList.isEmpty()) {
throw new IOExceptionList(directory.toString(), causeList);
}
}
org.apache.commons.io.FileUtils#forceDelete
/**
* Deletes a file or directory. For a directory, delete it and all sub-directories.
* 删除文件或目录,如果是目录,删除并删除所有子目录
* <p>
* The difference between File.delete() and this method are:
* </p>
* <ul>
* <li>The directory does not have to be empty.</li>
* 目录必须非空
* <li>You get an exception when a file or directory cannot be deleted.</li>
* 当文件或目录不能删除会跑出一个异常
* </ul>
*
* @param file file or directory to delete, must not be {@code null}.
* @throws NullPointerException if the file is {@code null}.
* @throws FileNotFoundException if the file was not found.
* @throws IOException in case deletion is unsuccessful.
*/
public static void forceDelete(final File file) throws IOException {
Objects.requireNonNull(file, "file");
final Counters.PathCounters deleteCounters;
try {
deleteCounters = PathUtils.delete(file.toPath(), PathUtils.EMPTY_LINK_OPTION_ARRAY,
StandardDeleteOption.OVERRIDE_READ_ONLY);
} catch (final IOException e) {
throw new IOException("Cannot delete file: " + file, e);
}
if (deleteCounters.getFileCounter().get() < 1 && deleteCounters.getDirectoryCounter().get() < 1) {
// didn't find a file to delete.
throw new FileNotFoundException("File does not exist: " + file);
}
}
org.apache.commons.io.file.PathUtils#delete(java.nio.file.Path, java.nio.file.LinkOption[], org.apache.commons.io.file.DeleteOption...)
/**
* Deletes a file or directory. If the path is a directory, delete it and all sub-directories.
* <p>
* The difference between File.delete() and this method are:
* </p>
* <ul>
* <li>A directory to delete does not have to be empty.</li>
* <li>You get exceptions when a file or directory cannot be deleted; {@link java.io.File#delete()} returns a
* boolean.
* </ul>
*
* @param path file or directory to delete, must not be {@code null}
* @param linkOptions How to handle symbolic links.
* @param deleteOptions How to handle deletion.
* @return The visitor used to delete the given directory.
* @throws NullPointerException if the directory is {@code null}
* @throws IOException if an I/O error is thrown by a visitor method or if an I/O error occurs.
* @since 2.9.0
*/
public static PathCounters delete(final Path path, final LinkOption[] linkOptions,
final DeleteOption... deleteOptions) throws IOException {
// File deletion through Files deletes links, not targets, so use LinkOption.NOFOLLOW_LINKS.
return Files.isDirectory(path, linkOptions) ? deleteDirectory(path, linkOptions, deleteOptions)
: deleteFile(path, linkOptions, deleteOptions);
}