|
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;
|
@@ -500,15 +499,16 @@ public static void replaceStringInFile(String filePath, String regex, String rep
|
500 | 499 | throws IOException {
|
501 | 500 | LoggingFacade logger = getLogger();
|
502 | 501 | Path src = Paths.get(filePath);
|
503 |
| - logger.info("Replacing {0} in {1}", regex, src.toString()); |
504 |
| - Charset charset = StandardCharsets.UTF_8; |
505 |
| - String content = new String(Files.readAllBytes(src), charset); |
506 |
| - String newcontent = content.replaceAll(regex, replacement); |
507 |
| - logger.info("with {0}", replacement); |
508 |
| - if (content.equals(newcontent)) { |
| 502 | + logger.info("Replacing {0} in {1} with {2}", regex, src.toString(), replacement); |
| 503 | + String content = new String(Files.readAllBytes(src), StandardCharsets.UTF_8); |
| 504 | + if (!content.contains(regex)) { |
509 | 505 | logger.info("search string {0} not found to replace with {1}", regex, replacement);
|
510 | 506 | }
|
511 |
| - Files.write(src, newcontent.getBytes(charset)); |
| 507 | + long oldModified = src.toFile().lastModified(); |
| 508 | + Files.write(src, content.replaceAll(regex, replacement).getBytes(StandardCharsets.UTF_8)); |
| 509 | + if (oldModified == src.toFile().lastModified()) { |
| 510 | + logger.info("No modification was done to the file"); |
| 511 | + } |
512 | 512 | }
|
513 | 513 |
|
514 | 514 | /**
|
|
0 commit comments