2

I'm trying to use Java WatchEvent ENTRY_MODIFY to check if a file is being access (ie: read, copied to clipboard). However from the documentation and a small test case I've made, that event isn't being fired. It's only fired when the file is changed.

Am I doing something wrong? If so, how can I monitor a file on the filesystem?

Silva
  • 270
  • 4
  • 16
  • what is your OS? Java version? Can you share some code? – jdiver May 21 '14 at 17:50
  • I don't have code yet because I'm still looking for the concept to monitor a file. My OS is windows and I'm using Java 7. – Silva May 21 '14 at 18:00
  • 1
    This sample code works. http://docs.oracle.com/javase/tutorial/essential/io/notification.html – jdiver May 21 '14 at 18:01
  • I'm sorry but it doesn't do what I need. I'm tried this sample http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java and while it still tells me if a file was modified, I still can open the file on a notepad (for instance) and see its content without any message from WatchDir. – Silva May 21 '14 at 18:51

3 Answers3

1

This isn't directly built into java. Your best bet is to jump into a native OS solution. This can be tedious if you want to support multiple systems though.

If you can get away with supporting windows take a look at THIS LINK . Scroll down to the bottom and look at similar apps. You would be interested in any app that contains a command line interface. What you will need to do is install one of the software and then kick off a process using Runtime.exec. You could potentially just use a direct dll, but I'm not qualified to tell you which dll will give you that information or if it even exists. It might be something you want to look into though if you do not want a 3rd party dependency.

You will read the results of the process that hooks into the windows dll's and will tell you if the file is currently open (See this link for more details). Your application will have to pull data (consistently asking the Application if the file is open). It is not ideal, but probably it is a potential solution.

0

Answering from your definition of file being accessed (copied and being read), however for file alteration there are several existing API available. Here is an example given to monitor file alteration.

To check file is copied to clipboard, you can use Clipboard#hasFiles() method when content of clipboard modified. If it returns true than file is copied to clipboard.

To check file is being read currently, you can check if the file is locked or not using implementation of FileLock abstract class. It has acquiredBy() method which returns the channel currently holding the lock on file.

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
  • 1
    I'm sorry, but my definition of being read is literal. Imagine a XML file being opened on notedpad and being literally read by the user. This situation has no Lock to rely on. – Silva May 27 '14 at 12:32
  • As for the clipboard, we can copy a file with a simple copy command on a shell. No clipboard messing around to do a hasFiles(). – Silva May 27 '14 at 12:33
0

you can try other libraries to accomplish that task, for example http://jnotify.sourceforge.net/ or http://java.dzone.com/announcements/new-java-library-monitor-file the latter specifically stands: File Access Monitoring- You will be able to receive notifications about events when access or modification date is changed.

Paizo
  • 3,986
  • 30
  • 45