1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. public class LogFileReplacer {
  7. public static void main(String[] args) {
  8. String logFilePath = "input.log"; // Replace with your log file path
  9. String replacementValue = "REPLACED"; // Replace with the value to replace
  10. String outputFilePath = "output.log"; // Replace with your desired output file path
  11. try {
  12. replaceInLogFile(logFilePath, replacementValue, outputFilePath);
  13. System.out.println("Log file processed successfully. Output written to: " + outputFilePath);
  14. } catch (IOException e) {
  15. System.err.println("An error occurred: " + e.getMessage());
  16. }
  17. }
  18. public static void replaceInLogFile(String inputFilePath, String replacement, String outputFilePath) throws IOException {
  19. try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
  20. BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
  21. String line;
  22. while ((line = reader.readLine()) != null) {
  23. String replacedLine = line.replace(findValue(line), replacement);
  24. writer.write(replacedLine);
  25. writer.newLine();
  26. }
  27. }
  28. }
  29. // Helper function to find the value to replace in a line. This is a simple string search.
  30. private static String findValue(String line) {
  31. // This is a basic implementation. Consider more robust parsing if needed.
  32. return "oldValue"; // Replace with the value you want to find
  33. }
  34. }

Add your comment