1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. public class LogExtractor {
  9. public static void main(String[] args) {
  10. String logFilePath = "your_log_file.log"; // Replace with your log file path
  11. String delimiter = ","; // Replace with your delimiter if needed
  12. try {
  13. List<Map<String, String>> data = extractData(logFilePath, delimiter);
  14. // Process the extracted data
  15. for (Map<String, String> row : data) {
  16. System.out.println(row); // Or perform other data migration tasks
  17. }
  18. } catch (IOException e) {
  19. System.err.println("Error reading log file: " + e.getMessage());
  20. }
  21. }
  22. public static List<Map<String, String>> extractData(String logFilePath, String delimiter) throws IOException {
  23. List<Map<String, String>> data = new ArrayList<>();
  24. try (BufferedReader br = new BufferedReader(new FileReader(logFilePath))) {
  25. String line;
  26. String[] headers = null;
  27. // Read the header line
  28. if ((line = br.readLine()) != null) {
  29. headers = line.split(delimiter);
  30. }
  31. //If no headers, return empty list
  32. if (headers == null) {
  33. return data;
  34. }
  35. // Read each data line
  36. while ((line = br.readLine()) != null) {
  37. String[] values = line.split(delimiter);
  38. if (values.length == headers.length) { // Ensure consistent number of values
  39. Map<String, String> row = new HashMap<>();
  40. for (int i = 0; i < headers.length; i++) {
  41. row.put(headers[i], values[i]);
  42. }
  43. data.add(row);
  44. }
  45. }
  46. }
  47. return data;
  48. }
  49. }

Add your comment