@Test(expected = IOException.class) public void testDeleteWithException() throws IOException { Assume.assumeTrue( FileSystems.getDefault().supportedFileAttributeViews().contains("posix") ); Path tmpDir = Files.createTempDirectory( "FileUtilsTest" ); Path tmpDir2 = tmpDir.resolve("testdir1"); Files.createDirectories( tmpDir2 ); Path tmpFile = tmpDir2.resolve("testfile1.txt"); OutputStream stream = null; try { stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE ); stream.write( 1 ); stream.close( ); assertTrue( Files.exists( tmpFile ) ); stream = Files.newOutputStream( tmpFile, StandardOpenOption.APPEND, StandardOpenOption.CREATE ); stream.write( 1 ); Set<PosixFilePermission> perms = new HashSet<>( ); Files.setPosixFilePermissions( tmpFile, perms ); Files.setPosixFilePermissions( tmpDir2, perms ); FileUtils.deleteDirectory( tmpDir ); assertFalse( Files.exists( tmpDir ) ); assertFalse( Files.exists( tmpFile ) ); } finally { if (stream!=null) { stream.close(); } Set<PosixFilePermission> perms = new HashSet<>( ); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions( tmpDir2, perms ); Files.setPosixFilePermissions( tmpFile, perms ); Files.deleteIfExists( tmpFile ); Files.deleteIfExists( tmpDir2 ); Files.deleteIfExists( tmpDir ); } }
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);  }