import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class LogFileReplacer {
public static void main(String[] args) {
String logFilePath = "input.log"; // Replace with your log file path
String replacementValue = "REPLACED"; // Replace with the value to replace
String outputFilePath = "output.log"; // Replace with your desired output file path
try {
replaceInLogFile(logFilePath, replacementValue, outputFilePath);
System.out.println("Log file processed successfully. Output written to: " + outputFilePath);
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
public static void replaceInLogFile(String inputFilePath, String replacement, String outputFilePath) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
String replacedLine = line.replace(findValue(line), replacement);
writer.write(replacedLine);
writer.newLine();
}
}
}
// Helper function to find the value to replace in a line. This is a simple string search.
private static String findValue(String line) {
// This is a basic implementation. Consider more robust parsing if needed.
return "oldValue"; // Replace with the value you want to find
}
}
Add your comment