@Override public boolean deleteIfExists(final Path path, final DeleteOption... options) throws IllegalArgumentException, DirectoryNotEmptyException, IOException, SecurityException { return Files.deleteIfExists(path, options); }
@Override public boolean deleteIfExists(final Path path, final DeleteOption... options) throws IllegalArgumentException, DirectoryNotEmptyException, IOException, SecurityException { final boolean result = Files.deleteIfExists(path, options); try { Files.deleteIfExists(dot(path), options); } catch (Exception ex) { } if (path instanceof AttrHolder) { ((AttrHolder) path).getAttrStorage().clear(); } return result; }
@Test public void deleteIfExistsNull() { assertThatThrownBy(() -> Files.deleteIfExists(null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'path' should be not null!"); }
@Override public void delete(final Path path, final DeleteOption... options) throws IllegalArgumentException, NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException { Files.delete(path, options); try { Files.deleteIfExists(dot(path), options); } catch (Exception ex) { } if (path instanceof AttrHolder) { ((AttrHolder) path).getAttrStorage().clear(); } }
@Test public void deleteIfExistsDirectoryNotEmpty() { final Path dir = newTempDir(); Files.createFile(dir.resolve("file.temp.txt")); assertThatThrownBy(() -> Files.deleteIfExists(dir)) .isInstanceOf(DirectoryNotEmptyException.class); }
public static long copy(final InputStream in, final Path target, final CopyOption... options) throws IOException, FileAlreadyExistsException, DirectoryNotEmptyException, UnsupportedOperationException, SecurityException { checkNotNull("in", in); checkNotNull("target", target); checkNotNull("options", options); boolean replaceExisting = false; for (final CopyOption opt : options) { if (opt == StandardCopyOption.REPLACE_EXISTING) { replaceExisting = true; break; } else { checkNotNull("opt", opt); throw new UnsupportedOperationException(opt + " not supported"); } } if (replaceExisting) { deleteIfExists(target); } final OutputStream out = newOutputStream(target, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); try { return internalCopy(in, out); } finally { try { out.close(); } catch (java.io.IOException e) { throw new IOException(e); } } }
@Test public void deleteIfExists() { final Path path = Files.createFile(newTempDir().resolve("file.temp.txt")); assertThat(path).isNotNull(); assertThat(path.toFile()).exists(); assertThat(Files.deleteIfExists(path)).isTrue(); assertThat(path).isNotNull(); assertThat(path.toFile()).doesNotExist(); final Path dir = newTempDir(); assertThat(dir).isNotNull(); assertThat(dir.toFile()).exists(); assertThat(Files.deleteIfExists(dir)).isTrue(); assertThat(dir).isNotNull(); assertThat(dir.toFile()).doesNotExist(); assertThat(Files.deleteIfExists(newTempDir().resolve("file.temp.txt"))).isFalse(); }