1. import java.io.*;
  2. import java.util.*;
  3. public class JsonMonitor {
  4. private final String configFilePath;
  5. private final List<String> jsonFiles;
  6. private final Map<String, Object> lastState = new HashMap<>(); // Store last known state
  7. private final Set<String> monitoredFiles = new HashSet<>();
  8. public JsonMonitor(String configFilePath) {
  9. this.configFilePath = configFilePath;
  10. loadJsonFiles();
  11. loadConfig();
  12. }
  13. private void loadJsonFiles() {
  14. try (BufferedReader reader = new BufferedReader(new FileReader(configFilePath))) {
  15. String line;
  16. while ((line = reader.readLine()) != null) {
  17. jsonFiles.add(line.trim());
  18. }
  19. } catch (IOException e) {
  20. System.err.println("Error loading JSON files: " + e.getMessage());
  21. }
  22. }
  23. private void loadConfig() {
  24. // Placeholder for config loading - can be extended to load other settings
  25. System.out.println("Config loaded: " + configFilePath);
  26. }
  27. public void startMonitoring() {
  28. for (String jsonFile : jsonFiles) {
  29. monitorFile(jsonFile);
  30. }
  31. }
  32. private void monitorFile(String jsonFile) {
  33. try {
  34. Object state = loadJsonFileContent(jsonFile);
  35. if (!lastState.containsKey(jsonFile) || !lastState.get(jsonFile).equals(state)) {
  36. System.out.println("State changed for file: " + jsonFile);
  37. lastState.put(jsonFile, state);
  38. }
  39. } catch (Exception e) {
  40. System.err.println("Error monitoring file " + jsonFile + ": " + e.getMessage());
  41. }
  42. }
  43. private Object loadJsonFileContent(String filePath) throws IOException {
  44. try (FileReader reader = new FileReader(filePath)) {
  45. // In a real application, you would parse the JSON here.
  46. // This example just reads the file content as a string.
  47. String jsonString = readAllText(reader);
  48. return jsonString;
  49. }
  50. }
  51. private String readAllText(FileReader reader) throws IOException {
  52. StringBuilder sb = new StringBuilder();
  53. int c;
  54. while ((c = reader.read()) != -1) {
  55. sb.append((char) c);
  56. }
  57. return sb.toString();
  58. }
  59. public static void main(String[] args) {
  60. // Example usage
  61. String configFilePath = "config.txt";
  62. JsonMonitor monitor = new JsonMonitor(configFilePath);
  63. monitor.startMonitoring();
  64. }
  65. }

Add your comment