I was wondering if there is a way to monitor the file system in Java.
Suppose the following
C:\Users\foobar\Desktop\notes.txt
I'd like to monitor which Java process accesses this file and when. The end goal is to return the Class and if possible the method.
Pseudocode example showing what I want to do
public/private class FileReader {
private void getFileContents(String fileName) throws Exception {
...
FileReader fileReader = Znew FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
...
}
Separate unrelated Class
public class Monitor {
...thread...
onFileAccessed() {
System.out.println(processID);
System.out.println(jarName);
System.out.println(className);
System.out.println(methodName);
}
...thread...
}
tyia!