@Test public void testDeleteNonExist() throws IOException { Path tf = Paths.get("aaserijdmcjdjhdejeidmdjdlasrjerjnbmckdkdk"); assertFalse(Files.exists(tf)); FileUtils.deleteDirectory( tf ); }
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);  }