이는 내가하는 데 문제가있어 내가 적응하는 방법을 알고하지 않는 파일 이름을 다음과 같이 주로 작동정규식 기능 이름 바꾸기 파일 문제
public static String getNewNameForCopyFile(final String originalName, final boolean firstCall) {
if (firstCall) {
final Pattern p = Pattern.compile("(.*?)(\\..*)?");
final Matcher m = p.matcher(originalName);
if (m.matches()) { //group 1 is the name, group 2 is the extension
String name = m.group(1);
String extension = m.group(2);
if (extension == null) {
extension = "";
}
return name + "-Copy1" + extension;
} else {
throw new IllegalArgumentException();
}
} else {
final Pattern p = Pattern.compile("(.*?)(-Copy(\\d+))?(\\..*)?");
final Matcher m = p.matcher(originalName);
if (m.matches()) { //group 1 is the prefix, group 2 is the number, group 3 is the suffix
String prefix = m.group(1);
String numberMatch = m.group(3);
String suffix = m.group(4);
return prefix + "-Copy" + (numberMatch == null ? 1 : (Integer.parseInt(numberMatch) + 1)) + (suffix == null ? "" : suffix);
} else {
throw new IllegalArgumentException();
}
}
}
을 내 코드 : test.abc.txt 이름이 바뀐 파일은 'test-Copy1.abc.txt'가되지만 'test.abc-Copy1.txt'여야합니다.
내 방법으로 어떻게 이것을 수행 할 수 있습니까?
'return prefix + "." + suffix + "-Copy"+ numberMatch + ".txt"' – msrd0