title: Monitor which Java process and which class/method accesses a specifc file
body: <p>I was wondering if there is a way to monitor the file system in Java.</p>
  
  <p>Suppose the following</p>
  
  <pre><code>C:\Users\foobar\Desktop\notes.txt
  </code></pre>
  
  <p>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.</p>
  
  <p>Pseudocode example showing what I want to do</p>
  
  <pre><code>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);
      }
   ...   
  }
  </code></pre>
  
  <p>Separate unrelated Class</p>
  
  <pre><code>public class Monitor {
    ...thread...
    onFileAccessed() {
      System.out.println(processID);
      System.out.println(jarName);
      System.out.println(className);
      System.out.println(methodName);
    }
    ...thread...
  }
  </code></pre>
  
  <p>tyia!</p>