I want to sync a folder like Dropbox. If in my Folder is changed a file or a folder, I want to get an Event, which starts my synchronisation Class. How can I get such an Event without scanning this folder by an Intervall?
You have at least two options. You can either reinvent the wheel as Arpit suggested or you can also use the WatchService API.
You can find a WatchService tutorial here.
Some start for you:
get the list of all files : yourdir.listFiles()
now for each file in filelist:
file.getLastModified()
if it is equal to current time or differ from lastSynctime(you need to maintain it in your sync class) then sync it.
Related
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?
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.
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.
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.
Im reading a log file in Java on a Linux box on a continual schedule of 2 minutes looking for certain messages. I store the last offset (RandomAccessFile getFilePointer) and read from it onwards when I detect LastModified has changed; is this best practice or even right?
If you are using Java 7, then you can use WatchService to notify you when a File inside of a directory changes. This is similar to interrupts and doesn't need continuous polling. See this for more details on WatchService. Otherwise you are better off with the polling method you already do.
Try this Tailer:
http://commons.apache.org/io/apidocs/org/apache/commons/io/input/Tailer.html
This is Java implementation of "tail -f" Unix functionality.
For detecting changes on files, or changes in any content inside directory, you can watch that directory for new content, modification of existing content and deletion of any existing content inside any directory.
See post: Directory watching for changes in java
I want to keep track of multiple files in a location in a unix box, and delete them if no one is using them for a long time.
I am trying to search for reference counting but did not get much help on google.
I also saw FileTime in java 7 which can give me the last accessed time , but i have to the above work using java 6.
If anyone has any ideas or good reference to reference counting and how i can use it, it will be great.
You can use apache common IO. Set observer on your dir which checks the dir and notifying listeners of create, change or delete eventslisten. By this it is possible to track on which dir has been working by user and others are not.
Okay, so we want last access time.
You can have a read of Get the Last Access Time for a File for some further info.
You could try and have a look at http://jdevel.wordpress.com/2011/04/08/file-last-access-time-in-java-on-linux/ for a possible soultion, but this is Linux, not Unix.
Finally, you could borrow the same idea and use JNA or JNI
There is no module for File watching till JDK 1.6. What you can do is, you can write your own file watcher by seeing the
lastModified() method of the java.io.File class.
It returns you the last modified time as a long value and you keep a watch on this file during a regular interval. If the total time difference
(presentTime - lastModifiedTime)
exceeds you time criteria, you can delete the file.
I want to monitor a directory, and when a file appears there open it, process it and then move it to another directory. The problem is how to check that the other program is done writing it. With Java 7, I can use a WatchService from FileSystem, but I can only check when the files are created. What I want is to know when all file handles are released.
My first thought was that I could obtain an exclusive lock, but it turned out that it was possible to kick out another application while it was actually updating the file.
What is the preferred way to do this in Java? Thanks!
The Watcher APIs currently allow you to see events when a file system object is created, modified or deleted in a watched directory. They don't tell you about other inotify events (on Linux). In fact, I don't think there is a way to do this in pure Java.
I was looking for a way to do this myself a few weeks ago and I came across a mail thread that suggested that you could write a custom implementation of the FileSystem api that provided a file watcher that supported other file system events. I decided not to pursue it because I had an alternative solution ... based on knowledge of how the files I am watching are being produced.
In my case, the files are produced by instruments that save image files to a shared drive. The solution is to watch the stream of "modified" events for a newly created file. When it stops and no more have been forthcoming for a couple of seconds (the "settling time"), then the file can be processed.
If this solution proves to be unreliable, the fallback is to implement the watching and initial processing (taking a snapshot of the file) in C / C++ using the inotify calls directly. This will allow me to directly observe the file close event.
The simplest way for a filebased interface is:
The sender writes the files with a changed filename (e.g. "example.xml_")
When the sender has finished writing the file, he renames it (e.g. "example.xml_" to "example.xml")
The receiver scans only for "*.xml"
I have a button that I want to disable as long as there isn't a specific number of files in a directory.
Is there some kind of listener that notifies me at the moment a file is created or deleted in a directory?
There's no current native support in Java for file system events and monitoring. JNotify is a useful library for doing this. You should set it up to monitor the directory for modifications, and then determine yourself what's been added/removed.
Java 7 will have file system event support built into it.
One thing you might want to consider - If you're listening for creation events then you'll want to make sure that the file is completely written before you start reading it. I'm not sure what type of support Java 7 will offer for this problem.
I've implemented mechanisms like this in the past and this particular problem required special handling. If you are controlling both the file reader and writer then you can get around this with naming conventions, etc. (the writer names the file xxx.prt and renames the file when it's done being written). Since I didn't have control of the writer I had to add another polling mechanism to check the file size on an interval to make sure each new file was actually ready to be read. Not a perfect solution, but was sufficient for my case.
My two cents...