Skip to content

Instantly share code, notes, and snippets.

@froop
Created October 2, 2011 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save froop/1256965 to your computer and use it in GitHub Desktop.
Save froop/1256965 to your computer and use it in GitHub Desktop.
[Java] 既に存在する名前だったら末尾に連番を付加
public String toUniqueName(String srcName, final List<String> existing) {
return toUniqueName(srcName, "", new NameChecker() {
@Override
public boolean exists(String name) {
return existing.contains(name);
}
});
}
public File toUniqueFile(File file) {
String basePath = file.getParent() + File.separator
+ getBaseName(file.getName());
String extension = getExtension(file.getName());
String suffix = "";
if (!"".equals(extension)) {
suffix = "." + extension;
}
String uniquePath = toUniqueName(basePath, suffix, new NameChecker() {
@Override
public boolean exists(String name) {
return new File(name).exists();
}
});
return new File(uniquePath);
}
private String toUniqueName(String srcName, String suffix, NameChecker checker) {
String retName = srcName + suffix;
for (int i = 2; checker.exists(retName); i++) {
retName = srcName + "(" + i + ")" + suffix;
}
return retName;
}
private interface NameChecker {
boolean exists(String name);
}
private String getBaseName(String fileName) {
int dotIdx = fileName.lastIndexOf('.');
if (dotIdx < 0) {
return fileName;
}
return fileName.substring(0, dotIdx);
}
private String getExtension(String fileName) {
int dotIdx = fileName.lastIndexOf('.');
if (dotIdx < 0) {
return "";
}
return fileName.substring(dotIdx + 1, fileName.length());
}
@Test
public void testToUniqueName() {
List<String> names = Arrays.asList("nameA", "nameB", "nameB(2)");
assertEquals("nameA(2)", toUniqueName("nameA", names));
assertEquals("nameB(3)", toUniqueName("nameB", names));
assertEquals("nameC", toUniqueName("nameC", names));
}
@Test
public void testToUniqueFile() throws IOException {
final String baseName = System.getProperty("java.io.tmpdir") + "testfile";
final String suffix = ".csv";
File file = new File(baseName + suffix);
File file1 = toUniqueFile(file);
assertEquals(baseName + suffix, file1.getPath());
file1.createNewFile();
file1.deleteOnExit();
File file2 = toUniqueFile(file);
assertEquals(baseName + "(2)" + suffix, file2.getPath());
file2.createNewFile();
file2.deleteOnExit();
File file3 = toUniqueFile(file);
assertEquals(baseName + "(3)" + suffix, file3.getPath());
}
@Test
public void testToUniqueFileNoExt() throws IOException {
final String baseName = System.getProperty("java.io.tmpdir") + "testnoext";
File file = new File(baseName);
File file1 = toUniqueFile(file);
assertEquals(baseName, file1.getPath());
file1.createNewFile();
file1.deleteOnExit();
File file2 = toUniqueFile(file);
assertEquals(baseName + "(2)", file2.getPath());
}
@froop
Copy link
Author

froop commented Oct 10, 2011

ファイル拡張子に対応、ファイル以外にもListに対応して汎用化

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment