public static Path createTempFile(final Path dir, final String prefix, final String suffix, 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(); final String realSufix; if (suffix != null && suffix.trim().length() > 0) { realSufix = normalizeSuffix(suffix); } else { realSufix = ".tmp"; } for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { try { return createFile(dir.resolve(baseName + counter + realSufix), 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) + ')'); }
return createFile(dir.resolve(baseName + counter + realSufix), attrs); } catch (Exception ex) {
return createFile(dir.resolve(baseName + counter + realSufix), attrs); } catch (Exception ex) {
@Test public void createFileNull() { assertThatThrownBy(() -> Files.createFile(null)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Parameter named 'path' should be not null!"); }
@Test public void createFileAlreadyExists() { assertThatThrownBy(() -> Files.createFile(Files.createTempFile("foo", "bar"))) .isInstanceOf(FileAlreadyExistsException.class); }
@Test public void deleteIfExistsDirectoryNotEmpty() { final Path dir = newTempDir(); Files.createFile(dir.resolve("file.temp.txt")); assertThatThrownBy(() -> Files.deleteIfExists(dir)) .isInstanceOf(DirectoryNotEmptyException.class); }
@Test public void deleteDirectoryNotEmpty() { final Path dir = newTempDir(); Files.createFile(dir.resolve("file.temp.txt")); assertThatThrownBy(() -> Files.delete(dir)) .isInstanceOf(DirectoryNotEmptyException.class); }
@Test public void createFile() { final Path path = Files.createFile(newTempDir().resolve("file.temp.txt")); assertThat(path).isNotNull(); assertThat(path.toFile()).exists(); }
@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(); }
@Test public void delete() { final Path path = Files.createFile(newTempDir().resolve("file.temp.txt")); assertThat(path).isNotNull(); assertThat(path.toFile()).exists(); Files.delete(path); assertThat(path).isNotNull(); assertThat(path.toFile()).doesNotExist(); final Path dir = newTempDir(); assertThat(dir).isNotNull(); assertThat(dir.toFile()).exists(); Files.delete(dir); assertThat(dir).isNotNull(); assertThat(dir.toFile()).doesNotExist(); }
@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(); }
@Test public void createDirectories() { final Path path = newTempDir(); final Path dir = Files.createDirectories(path.resolve("myNewDir/mysubDir1/mysubDir2")); 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(); }