@Override public Path createDirectory(final Path dir, final FileAttribute<?>... attrs) throws IllegalArgumentException, UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { return Files.createDirectory(dir, attrs); }
public static Path createTempDirectory(final Path dir, final String prefix, final FileAttribute<?>... attrs) throws IllegalArgumentException, UnsupportedOperationException, IOException, SecurityException { checkNotNull("dir", dir); if (notExists(dir)) { throw new NoSuchFileException(dir.toString()); } final StringBuilder sb = new StringBuilder(); if (prefix != null && prefix.trim().length() > 0) { sb.append(prefix).append("-"); } final String baseName = sb.append(System.currentTimeMillis()).append("-").toString(); for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { final Path path2Create = dir.resolve(baseName + counter); try { return createDirectory(path2Create, attrs); } catch (Exception ex) { } } throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried " + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')'); }
public static Path createTempDirectory(final Path dir, final String prefix, final FileAttribute<?>... attrs) throws IllegalArgumentException, UnsupportedOperationException, IOException, SecurityException { checkNotNull("dir", dir); if (notExists(dir)) { throw new NoSuchFileException(dir.toString()); } final StringBuilder sb = new StringBuilder(); if (prefix != null && prefix.trim().length() > 0) { sb.append(prefix).append("-"); } final String baseName = sb.append(System.currentTimeMillis()).append("-").toString(); for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { final Path path2Create = dir.resolve(baseName + counter); try { return createDirectory(path2Create, attrs); } catch (Exception ex) { } } throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried " + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')'); }
public static Path createTempDirectory(final Path dir, final String prefix, final FileAttribute<?>... attrs) throws IllegalArgumentException, UnsupportedOperationException, IOException, SecurityException { checkNotNull("dir", dir); if (notExists(dir)) { throw new NoSuchFileException(dir.toString()); } final StringBuilder sb = new StringBuilder(); if (prefix != null && prefix.trim().length() > 0) { sb.append(prefix).append("-"); } final String baseName = sb.append(System.currentTimeMillis()).append("-").toString(); for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { final Path path2Create = dir.resolve(baseName + counter); try { return createDirectory(path2Create, attrs); } catch (Exception ex) { } } throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried " + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')'); }
@Test public void createDirectoryNull() { assertThatThrownBy(() -> Files.createDirectory(null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'dir' should be not null!"); }
@Test public void createDirectoryFileAlreadyExists() { assertThatThrownBy(() -> Files.createDirectory(newTempDir())) .isInstanceOf(FileAlreadyExistsException.class); }
protected Path internalCreateDirectory(final Path dir, final boolean skipAlreadyExistsException, final FileAttribute<?>... attrs) throws IllegalArgumentException, UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull("dir", dir); FileAttribute<?>[] allAttrs = attrs; try { Files.createDirectory(dir, attrs); } catch (final FileAlreadyExistsException ex) { final Properties properties = new Properties(); if (exists(dot(dir))) { properties.load(newInputStream(dot(dir))); } allAttrs = consolidate(properties, attrs); if (!skipAlreadyExistsException) { throw ex; } } buildDotFile(dir, newOutputStream(dot(dir)), allAttrs); return dir; }
@Test public void createDirectory() { final Path path = newTempDir(); final Path dir = Files.createDirectory(path.resolve("myNewDir")); assertThat(dir).isNotNull(); assertThat(dir.toFile()).exists(); assertThat(dir.toFile()).isDirectory(); final Path file = Files.createFile(dir.resolve("new.file.txt")); assertThat(file).isNotNull(); assertThat(file.toFile()).exists(); assertThat(file.toFile()).isFile(); }