|
7 | 7 | import java.io.FileNotFoundException;
|
8 | 8 | import java.io.FileOutputStream;
|
9 | 9 | import java.io.IOException;
|
10 |
| -import java.nio.charset.Charset; |
11 | 10 | import java.nio.charset.StandardCharsets;
|
12 | 11 | import java.nio.file.FileVisitResult;
|
13 | 12 | import java.nio.file.Files;
|
@@ -469,15 +468,16 @@ public static void replaceStringInFile(String filePath, String regex, String rep
|
469 | 468 | throws IOException {
|
470 | 469 | LoggingFacade logger = getLogger();
|
471 | 470 | Path src = Paths.get(filePath);
|
472 |
| - logger.info("Replacing {0} in {1}", regex, src.toString()); |
473 |
| - Charset charset = StandardCharsets.UTF_8; |
474 |
| - String content = new String(Files.readAllBytes(src), charset); |
475 |
| - String newcontent = content.replaceAll(regex, replacement); |
476 |
| - logger.info("with {0}", replacement); |
477 |
| - if (content.equals(newcontent)) { |
| 471 | + logger.info("Replacing {0} in {1} with {2}", regex, src.toString(), replacement); |
| 472 | + String content = new String(Files.readAllBytes(src), StandardCharsets.UTF_8); |
| 473 | + if (!content.contains(regex)) { |
478 | 474 | logger.info("search string {0} not found to replace with {1}", regex, replacement);
|
479 | 475 | }
|
480 |
| - Files.write(src, newcontent.getBytes(charset)); |
| 476 | + long oldModified = src.toFile().lastModified(); |
| 477 | + Files.write(src, content.replaceAll(regex, replacement).getBytes(StandardCharsets.UTF_8)); |
| 478 | + if (oldModified == src.toFile().lastModified()) { |
| 479 | + logger.info("No modification was done to the file"); |
| 480 | + } |
481 | 481 | }
|
482 | 482 |
|
483 | 483 | /**
|
|
0 commit comments