@Test public void testDelete() throws IOException { Path td = Files.createTempDirectory( "FileUtilsTest" ); Path f1 = td.resolve("file1.txt"); Path f2 = td.resolve("file2.txt"); Path d1 = td.resolve("dir1"); Files.createDirectory( d1 ); Path d11 = d1.resolve("dir11"); Files.createDirectory( d11 ); Path f111 = d11.resolve("file111.txt"); Path f112 = d11.resolve("file112.txt"); Files.write(f1,"file1".getBytes()); Files.write(f2, "file2".getBytes()); Files.write(f111, "file111".getBytes()); Files.write(f112, "file112".getBytes()); assertTrue(Files.exists(d1)); assertTrue(Files.exists(f1)); assertTrue(Files.exists(f2)); assertTrue(Files.exists(f111)); assertTrue(Files.exists(f112)); FileUtils.deleteDirectory( td ); assertFalse(Files.exists(f1)); assertFalse(Files.exists(f2)); assertFalse(Files.exists(f111)); assertFalse(Files.exists(f112)); assertFalse(Files.exists(d1)); }
public static void deleteDirectory(Path dir) throws IOException { if (!Files.exists(dir)) { return; } if (!Files.isDirectory(dir)) { throw new IOException("Given path is not a directory " + dir); } boolean result = true; try(Stream<Path> stream = Files.walk(dir)) { result = stream .sorted( Comparator.reverseOrder() ) .map(file -> { try { Files.delete(file); return Optional.of(Boolean.TRUE); } catch (UncheckedIOException | IOException e) { log.warn("File could not be deleted {}: {}", file, e.getMessage()); return Optional.empty(); } }).allMatch(Optional::isPresent); } catch (UncheckedIOException e) { throw new IOException("File deletion failed ", e); } if (!result) { throw new IOException("Error during recursive delete of " + dir.toAbsolutePath()); } }
FileUtils { public static void deleteDirectory(Path dir) throws IOException { if (!Files.exists(dir)) { return; } if (!Files.isDirectory(dir)) { throw new IOException("Given path is not a directory " + dir); } boolean result = true; try(Stream<Path> stream = Files.walk(dir)) { result = stream .sorted( Comparator.reverseOrder() ) .map(file -> { try { Files.delete(file); return Optional.of(Boolean.TRUE); } catch (UncheckedIOException | IOException e) { log.warn("File could not be deleted {}: {}", file, e.getMessage()); return Optional.empty(); } }).allMatch(Optional::isPresent); } catch (UncheckedIOException e) { throw new IOException("File deletion failed ", e); } if (!result) { throw new IOException("Error during recursive delete of " + dir.toAbsolutePath()); } } }
FileUtils { public static void deleteDirectory(Path dir) throws IOException { if (!Files.exists(dir)) { return; } if (!Files.isDirectory(dir)) { throw new IOException("Given path is not a directory " + dir); } boolean result = true; try(Stream<Path> stream = Files.walk(dir)) { result = stream .sorted( Comparator.reverseOrder() ) .map(file -> { try { Files.delete(file); return Optional.of(Boolean.TRUE); } catch (UncheckedIOException | IOException e) { log.warn("File could not be deleted {}: {}", file, e.getMessage()); return Optional.empty(); } }).allMatch(Optional::isPresent); } catch (UncheckedIOException e) { throw new IOException("File deletion failed ", e); } if (!result) { throw new IOException("Error during recursive delete of " + dir.toAbsolutePath()); } }  }
FileUtils { public static void deleteDirectory(Path dir) throws IOException { if (!Files.exists(dir)) { return; } if (!Files.isDirectory(dir)) { throw new IOException("Given path is not a directory " + dir); } boolean result = true; try(Stream<Path> stream = Files.walk(dir)) { result = stream .sorted( Comparator.reverseOrder() ) .map(file -> { try { Files.delete(file); return Optional.of(Boolean.TRUE); } catch (UncheckedIOException | IOException e) { log.warn("File could not be deleted {}: {}", file, e.getMessage()); return Optional.empty(); } }).allMatch(Optional::isPresent); } catch (UncheckedIOException e) { throw new IOException("File deletion failed ", e); } if (!result) { throw new IOException("Error during recursive delete of " + dir.toAbsolutePath()); } }  static void deleteQuietly(Path dir); static IOStatus deleteDirectoryWithStatus(Path dir); static void deleteDirectory(Path dir); static String readFileToString(Path file, Charset encoding); static void writeStringToFile(Path file, Charset encoding, String value); static String getBasedir(); Path resolveNonAbsolute(Path parent, String child); static void unzipFileEntry(ZipFile file, ZipEntry entry, Path destinationPath); static void unzip(Path zipFile, Path destinationPath); }
FileUtils { public static void deleteDirectory(Path dir) throws IOException { if (!Files.exists(dir)) { return; } if (!Files.isDirectory(dir)) { throw new IOException("Given path is not a directory " + dir); } boolean result = true; try(Stream<Path> stream = Files.walk(dir)) { result = stream .sorted( Comparator.reverseOrder() ) .map(file -> { try { Files.delete(file); return Optional.of(Boolean.TRUE); } catch (UncheckedIOException | IOException e) { log.warn("File could not be deleted {}: {}", file, e.getMessage()); return Optional.empty(); } }).allMatch(Optional::isPresent); } catch (UncheckedIOException e) { throw new IOException("File deletion failed ", e); } if (!result) { throw new IOException("Error during recursive delete of " + dir.toAbsolutePath()); } }  static void deleteQuietly(Path dir); static IOStatus deleteDirectoryWithStatus(Path dir); static void deleteDirectory(Path dir); static String readFileToString(Path file, Charset encoding); static void writeStringToFile(Path file, Charset encoding, String value); static String getBasedir(); Path resolveNonAbsolute(Path parent, String child); static void unzipFileEntry(ZipFile file, ZipEntry entry, Path destinationPath); static void unzip(Path zipFile, Path destinationPath);  }