Here you can find the source of createUniqueFilePath(final String directory, final String baseName, final String suffix)
Parameter | Description |
---|---|
directory | the directory where the file should be created. |
baseName | the base name. |
suffix | the suffix (starting with a period). |
public static String createUniqueFilePath(final String directory, final String baseName, final String suffix)
//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(); } }