|
10 | 10 | import java.util.Comparator;
|
11 | 11 | import java.util.Objects;
|
12 | 12 | import java.util.Optional;
|
13 |
| -import java.util.regex.Matcher; |
14 |
| -import java.util.regex.Pattern; |
15 | 13 | import java.util.stream.Stream;
|
16 | 14 |
|
17 | 15 | /**
|
@@ -173,25 +171,23 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
|
173 | 171 | PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("regex:" + baseName + "(-\\d+)?\\." + extension);
|
174 | 172 |
|
175 | 173 | try (Stream<Path> stream = Files.walk(targetPath, 1)) {
|
| 174 | + int base = baseName.length(); |
176 | 175 | int ext = extension.length() + 1;
|
177 | 176 |
|
178 |
| - Optional<String> optional = |
| 177 | + Optional<Integer> optional = |
179 | 178 | stream
|
180 | 179 | .map(Path::getFileName)
|
181 | 180 | .filter(pathMatcher::matches)
|
182 | 181 | .map(String::valueOf)
|
183 |
| - .map(s -> s.substring(0, s.length() - ext)) |
| 182 | + .map(s -> "0" + s.substring(base, s.length() - ext)) |
| 183 | + .map(s -> s.replace("0-", "")) |
| 184 | + .map(Integer::valueOf) |
| 185 | + .map(i -> i + 1) |
184 | 186 | .sorted(Comparator.reverseOrder())
|
185 | 187 | .findFirst();
|
186 | 188 |
|
187 | 189 | if (optional.isPresent()) {
|
188 |
| - int index = 1; |
189 |
| - Pattern pattern = Pattern.compile(baseName + "-(\\d+)"); |
190 |
| - Matcher matcher = pattern.matcher(optional.get()); |
191 |
| - if (matcher.matches()) { |
192 |
| - index = Integer.parseInt(matcher.group(1)); |
193 |
| - } |
194 |
| - newName = baseName + "-" + Integer.toString(index + 1) + "." + extension; |
| 190 | + newName = baseName + "-" + optional.get() + "." + extension; |
195 | 191 | } else {
|
196 | 192 | newName = baseName + "." + extension;
|
197 | 193 | }
|
|
0 commit comments