FileUtils { public static void deleteDirectory(File directory) throws IOException { checkNotNull(directory, "directory"); if (directory.isDirectory()) { try { cleanDirectory(directory); } catch (FileNotFoundException ignored) { return; } try { Files.delete(directory.toPath()); } catch (NoSuchFileException ignored) { } } else if (directory.exists()) { throw new IOException(directory + " is not a directory"); } } private  FileUtils(); static String getRandomFilename(final String prefix); static String readFile(File file, String charsetName); static String readFileUtf8(File file); static void writeFile(File file, String contents, String encoding); static void writeFileUtf8(File file, String contents); static void deleteFileOrDirectory(File file); static void deleteDirectory(File directory); static void deleteDirectoryQuietly(File directory); static void cleanDirectory(File directory); static boolean deletePathIfEmpty(FileSystem fileSystem, Path path);  }
@Test public void testDeleteDirectory() throws Exception { File doesNotExist = new File(tmp.newFolder(), "abc"); FileUtils.deleteDirectory(doesNotExist); File cannotDeleteParent = tmp.newFolder(); File cannotDeleteChild = new File(cannotDeleteParent, "child"); try { assumeTrue(cannotDeleteChild.createNewFile()); assumeTrue(cannotDeleteParent.setWritable(false)); assumeTrue(cannotDeleteChild.setWritable(false)); FileUtils.deleteDirectory(cannotDeleteParent); fail("this should fail with an exception"); } catch (AccessDeniedException ignored) { } finally { cannotDeleteParent.setWritable(true); cannotDeleteChild.setWritable(true); } }
@Test public void testDeleteDirectoryWhichIsAFile() throws Exception { File file = tmp.newFile(); try { FileUtils.deleteDirectory(file); fail("this should fail with an exception"); } catch (IOException ignored) { } }