Java Path File Name nio makeUnique(Path basePath, String baseName)

Here you can find the source of makeUnique(Path basePath, String baseName)

Description

If the given path contains a file baseName a number is added as a suffix.

License

Apache License

Declaration

public static Path makeUnique(Path basePath, String baseName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**//from  w w  w.j a  v  a  2 s .  c o m
     * If the given path contains a file baseName a number is added as a suffix.
     */
    public static Path makeUnique(Path basePath, String baseName) {

        Path desiredPath = Paths.get(basePath.toString(), baseName);
        int suffix = 0;

        while (Files.exists(desiredPath)) {
            desiredPath = Paths.get(basePath.toString(), baseName + "_" + (suffix++));
        }

        return desiredPath;
    }
}

Related

  1. initializePath(String... envVarNames)
  2. isExecutableOnPath(String execName)
  3. isHiddenName(Path path)
  4. jobNameFromPath(Path path)
  5. listDocTypeName(Path path)
  6. pathToPackageName(Path path)
  7. readFileFromPath(String filename)
  8. relativizePath(Path relativeTo, String filename)
  9. rename(Path origin, Path destination)