monitor a Folder and extract all data in the file using java

370 Views Asked by At

am trying to monitor a folder/file for any changes and then extract all the data from the file and append it to the database using java.

i have tried the watch service api in java to monitor the file as shown in the code snippet below.

import static java.nio.file.StandardWatchEventKinds.*;
import java.io.*;
import java.util.*;

public class FolderMonitor {

    public void fileMonitor() throws IOException {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        Path path = Paths.get("C:/Users/xxxxx/Desktop/yyyyy");
        path.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
    }
}
  1. The above snippet monitors the file path and shows that in the console if a main function is added or called in another method.
  2. So now i need to be able to read the data from that file whenever it is created or modified and then append the data to the database

Any help is highly welcome

1

There are 1 best solutions below

0
mherman22 On

so tihs is how i achieved this, i created various classes as shown below.

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class FolderMonitor {
    
    public static void fileMonitor() {
        
        final String FOLDER_NAME = "xxxxxxxxxxx";
        final String FILE_NAME = "xxxxxx/filename";
        
        try {System.out.println("Watching directory for changes in the info file");
            WatchService watchService = FileSystems.getDefault().newWatchService();
            Path directory = Paths.get(FOLDER_NAME);
            WatchKey watchKey = directory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                    StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);

            while (true) {
                for (WatchEvent<?> event : watchKey.pollEvents()) {
                    @SuppressWarnings("unchecked")
                    WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;
                    Path fileName = pathEvent.context();
                    WatchEvent.Kind<?> kind = event.kind();

                    if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
                        System.out.println("Info file has been created : " + fileName);
                        FileReadingToJson.infoFileReading(FILE_NAME);
                    }

                    if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
                        System.out.println("The info file has been deleted: " + fileName);
                    }
                    
                    if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
                        FileReadingToJson.infoFileReading(FILE_NAME);
                    }
                }

                boolean valid = watchKey.reset();
                if (!valid) {
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And this is how i read the properties file and parsed it to json

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.json.JSONObject;
import org.json.Property;

public class FileReadingToJson {

    public static void infoFileReading(String filename) {
        try (InputStream input = new FileInputStream(filename)) {
            Properties infoFilepropeProperties = new Properties();
            infoFilepropeProperties.load(input);
            
            JSONObject jsonObject = Property.toJSONObject(infoFilepropeProperties);
            String jsonObjectstring = jsonObject.toString();
            System.out.println(jsonObject);
               
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

Currently those snippets do the monitoring and reading of the properties file and parse them to json format.

Now am remaining with using that json data to populate the database whenever there is change in the file.