Java Path File Name nio findNextFileName(Path folder, String name)

Here you can find the source of findNextFileName(Path folder, String name)

Description

find Next File Name

License

Open Source License

Declaration

public static String findNextFileName(Path folder, String name) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String findNextFileName(Path folder, String name) {
        Path file = folder.resolve(name);
        if (!Files.exists(file)) {
            return name;
        }/*from   w  w w.j av  a 2 s  .  c  o  m*/

        String a = getNamePart(file);
        String b = getExtPart(file);
        if (b.length() > 0) {
            b = "." + b;
        }
        int i = 1;
        while (Files.exists(folder.resolve(a + i + b))) {
            i++;
        }
        return a + i + b;
    }

    public static String getNamePart(Path file) {
        String s = file.getFileName().toString();
        return getNamePart(s);
    }

    public static String getNamePart(String filename) {
        int i = filename.lastIndexOf('.');
        if (i == -1) {
            return filename;
        }

        return filename.substring(0, i);
    }

    public static String getExtPart(Path file) {
        String s = file.getFileName().toString();
        return getExtPart(s);
    }

    public static String getExtPart(String filename) {
        int i = filename.lastIndexOf('.');
        if (i == -1) {
            return "";
        }
        return filename.substring(i + 1);
    }
}

Related

  1. fileName(final Path thePath)
  2. filterJarContainingClass(Collection jarsPaths, String className)
  3. findExecutablePaths(String executableName)
  4. findFile(Path directoryPath, String fileName)
  5. findFile(String basePath, final String fileName)
  6. findRootPathForResource(String resourceName, ClassLoader classLoader)
  7. findUpward(String filename, Path startingPath)
  8. formatPathName(Path path)
  9. getAbsolutePath(String fileName)