Java Path File Name nio getUniqueFileInDirectory(Path dir, String name)

Here you can find the source of getUniqueFileInDirectory(Path dir, String name)

Description

get Unique File In Directory

License

Apache License

Declaration

public static Path getUniqueFileInDirectory(Path dir, String name) 

Method Source Code

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

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

public class Main {
    public static Path getUniqueFileInDirectory(Path dir, String name) {
        int extension = name.lastIndexOf('.');
        return extension <= 0 || extension >= name.length() - 1 ? getUniqueFileInDirectory(dir, name, "")
                : getUniqueFileInDirectory(dir, name.substring(0, extension), name.substring(extension));
    }/*from www  . j a va  2  s .  c  o m*/

    public static Path getUniqueFileInDirectory(Path dir, String name, String extension) {
        Path file = dir.resolve(name + extension);
        if (Files.exists(file)) {
            file = dir.resolve(name + " copy" + extension);
            for (int i = 2; Files.exists(file); i++) {
                file = dir.resolve(String.format("%s copy %d%s", name, i, extension));
            }
        }
        return file;
    }
}

Related

  1. getRootPathFromDirectory(String resourceName, URL resource)
  2. getSimpleName(String path)
  3. getSolrCorePath(Path solrHome, String indexName)
  4. getSourceRoot(Path filePath, String pkgName)
  5. getTestDataPath(Class clazz, String name)
  6. inheritGwtModule(Path path, String inheritableModuleLogicalName)
  7. initializePath(String... envVarNames)
  8. isExecutableOnPath(String execName)
  9. isHiddenName(Path path)