Java Utililty Methods Path Create nio

List of utility methods to do Path Create nio

Description

The list of methods to do Path Create nio are organized into topic(s).

Method

PathcreateTempPath(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
return tempFile.toPath();
PathcreateTmpDir(Path tmpDir, String prefix)
create Tmp Dir
if (tmpDir == null) {
    return Files.createTempDirectory(prefix);
} else {
    return Files.createTempDirectory(tmpDir, prefix);
StringcreateUniqueFilePath(final String directory, final String baseName, final String suffix)
Create a unique file path from a directory, base name, and suffix.
Path filePath = Paths.get(directory, baseName + suffix);
int index = 1;
while (Files.exists(filePath)) {
    filePath = Paths.get(directory, baseName + "-" + index + suffix);
    index++;
return filePath.toString();
URLcreateUrlQuietly(Path path)
create Url Quietly
try {
    return path.toUri().toURL();
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
voidcreateZipArchive(final Path sourceDir, final Path destFile)
create Zip Archive
final byte[] buffer = new byte[4096]; 
final ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(destFile));
Files.walkFileTree(sourceDir, new FileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        return FileVisitResult.CONTINUE;
    @Override
...
FilecreateZipFile(final File pathToArchive)
Create an archive containing the path supplied, in a temp file, and all children if the path is a directory.
if (pathToArchive == null) {
    throw new RuntimeException("The path to archive cannot be null");
} else if (!pathToArchive.exists()) {
    throw new RuntimeException("The path to archive does not exist");
final File archive = File.createTempFile(UUID.randomUUID().toString(), ".zip");
try (final FileOutputStream fos = new FileOutputStream(archive);
        final ZipOutputStream zos = new ZipOutputStream(fos)) {
...
PathcreateZipFileSystem(Path path)
Create a zip file system and returns back the root
Map<String, String> env = new HashMap<>();
if (!Files.exists(path)) {
    env.put("create", "true");
URI uri = URI.create("jar:file:" + path.toUri().getPath());
FileSystem fileSystem = FileSystems.newFileSystem(uri, env, null);
return fileSystem.getPath("/");
FileSystemcreateZipFileSystem(Path zipFile)
create Zip File System
final URI uri = URI.create("jar:file:" + zipFile.toUri().getPath());
Map<String, ?> env = Collections.emptyMap();
return FileSystems.newFileSystem(uri, env);
FileSystemcreateZipFs(Path path)
Delete path if it exists and create a new zip at the location.
Files.deleteIfExists(path);
URI uri = new URI("jar:" + path.toUri());
return FileSystems.newFileSystem(uri, Collections.singletonMap("create", String.valueOf(true)));
StringcreateZkNodeName(String zkRoot, Path root, Path file)
create Zk Node Name
String relativePath = root.relativize(file).toString();
String separator = root.getFileSystem().getSeparator();
if ("\\".equals(separator))
    relativePath = relativePath.replaceAll("\\\\", "/");
if (relativePath.length() == 0)
    return zkRoot;
return zkRoot + "/" + relativePath;