FileTasks { public static void replaceLogFileContent(List<String> listToReplace, String replacement, Path logPath) { LOGGER.info("Check if content of " + logPath.getFileName().toString() + " needs to be replaced."); List<String> allLinesOfLogFile = readAllLines(logPath); if (allLinesOfLogFile.removeAll(listToReplace)) { try (PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(logPath))) { allLinesOfLogFile.forEach(printWriter::println); printWriter.println(replacement); LOGGER.info("Replaced " + listToReplace.toString() + " with " + replacement + " in " + logPath.getFileName().toString() + "."); } catch (IOException e) { LOGGER.warn("Error while writing to " + logPath.getFileName() + "."); } } }  static void createFile(Path file, String content); static void deleteDirectory(Path directory); static void mkdirs(Path dir); static void assertDirectory(Path dir); static void assertExecutableFile(Path p); static void assertFile(Path p); static void assertFileExtension(Path path, String extension); static List<String> readAllLines(Path path); static void deleteLine(Path path, int lineNumber); static void replaceLine(Path path, String oldContent, String newLineContent); static void deleteFile(Path file); static void copyFileIntoFolder(Path file, Path targetFolder); static void copyFileIntoFolderAndOverwrite(Path file, Path targetFolder); static void copyFileIntoFolderAndOverwrite(URL url, String filename, Path targetFolder); static void copyFileContentsToNewFile(Path source, Path target); static boolean hasFile(Path file); static boolean hasFolder(Path folder); static boolean hasNoFile(Path file); static boolean hasFileSpecificSubstring(Path path, String substring); static void move(Path from, Path to); static void moveFolderWithForcedCleanup(Path from, Path to); static Path findFirstMatchInFolder(Path folder, String glob); static List<Path> findAllInFolder(Path folder); static List<Path> findAllInFolder(Path folder, String glob); static void copyFilesInFolderIntoOtherFolder(Path from, Path to); static void replaceTokensInFile(Path targetFile, Map<String, ?> replacements); static void replaceTokensInFolder(Path targetFile, String token, Object replacement); static void replaceTokensInFolder(Path targetFile, Map<String, ?> replacements); static void replaceTokenInFile(Path targetFile, String token, String value); static String getFilenameWithoutExtension(Path file); static String getFilenameWithoutExtension(String filename); static String getFileExtension(Path filename); static void copyMatchingFilesIntoFolder(Path from, Path to, String globPattern); static void replaceLogFileContent(List<String> listToReplace, String replacement, Path logPath);  }
@Test public void testReplaceLogFileContent(){ List<String> listToReplace = new ArrayList<>(); listToReplace.add("error-deployment"); listToReplace.add("error-runtime"); List<String> listExpected = new ArrayList<>(); listExpected.add("task1"); listExpected.add("error-generic"); FileTasks.createFile(PATH_LOG_FILE, "error-deployment" + "\n" + "error-runtime" +"\n" + "task1"); FileTasks.replaceLogFileContent(listToReplace, "error-generic", PATH_LOG_FILE); assertEquals(listExpected, FileTasks.readAllLines(PATH_LOG_FILE)); }