Java Path Create nio createUniqueFilePath(final String directory, final String baseName, final String suffix)

Here you can find the source of createUniqueFilePath(final String directory, final String baseName, final String suffix)

Description

Create a unique file path from a directory, base name, and suffix.

License

Apache License

Parameter

Parameter Description
directory the directory where the file should be created.
baseName the base name.
suffix the suffix (starting with a period).

Return

a unique file path.

Declaration

public static String createUniqueFilePath(final String directory, final String baseName, final String suffix) 

Method Source Code

//package com.java2s;
/**//from w  ww.java2 s  .c o  m
 * Copyright 2014 VU University Medical Center.
 * Licensed under the Apache License version 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.html).
 */

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

public class Main {
    /**
     * Create a unique file path from a directory, base name, and suffix. While the file path exists, a minus and an
     * increasing number are added after the base name.
     *
     * @param directory the directory where the file should be created.
     * @param baseName the base name.
     * @param suffix the suffix (starting with a period).
     * @return a unique file path.
     */
    public static String createUniqueFilePath(final String directory, final String baseName, final String 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();
    }
}

Related

  1. createTempDirectory(Path dir, String prefix)
  2. createTempFile(Path baseDir, String prefix, String suffix)
  3. createTempFilePath(final String stem, final String extension)
  4. createTempPath(String prefix, String suffix)
  5. createTmpDir(Path tmpDir, String prefix)
  6. createUrlQuietly(Path path)
  7. createZipArchive(final Path sourceDir, final Path destFile)
  8. createZipFile(final File pathToArchive)
  9. createZipFileSystem(Path path)