Java nio file monitor




















Shyam says:. Bhushan says:. Vishal says:. Pradyumna Swain says:. Zahid Indher says:. Sunny says:. BPK says:. Vinayak says:. Mike says:. Durga Prasad reddy says:. Rahul Mahajan says:. Ranjith says:. Kenyan Stydent says:. YATK says:. Hats says:. Kishore says:. Baskar says:. Like Join the DZone community and get the full member experience. Join For Free. Learn more about watching files with Java NIO. You may also like: Java IO and NIO When the service detects an event of interest, it is forwarded to the registered process and handled as needed.

This is basically how it works: The first step is to create a new WatchService by using the newWatchService method of the FileSystem class. Next, we register a Path instance for the folder to be monitored with the types of events that we are interested in.

And at last, we implement an infinite loop to wait for incoming events. When an event occurs, the key is signaled and placed into the watcher's queue. After processing its events, we need to put it back into a ready state by invoking its reset method. If it returns false, the key is no longer valid and the loop can exit. Opinions expressed by DZone contributors are their own. If a thread is currently blocked in the take or poll methods waiting for a key to be queued then it immediately receives a ClosedWatchServiceException.

Any valid keys associated with this watch service are invalidated. After a watch service is closed, any further attempt to invoke operations upon it will throw ClosedWatchServiceException. If this watch service is already closed then invoking this method has no effect. Returns: the next watch key, or null Throws: ClosedWatchServiceException - if this watch service is closed poll WatchKey poll long timeout, TimeUnit unit throws InterruptedException Retrieves and removes the next watch key, waiting if necessary up to the specified wait time if none are yet present.

Parameters: timeout - how to wait before giving up, in units of unit unit - a TimeUnit determining how to interpret the timeout parameter Returns: the next watch key, or null Throws: ClosedWatchServiceException - if this watch service is closed, or it is closed while waiting for the next key InterruptedException - if interrupted while waiting take WatchKey take throws InterruptedException Retrieves and removes next watch key, waiting if none are yet present.

That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy. This API enables you to register a directory or directories with the watch service.

When registering, you tell the service which types of events you are interested in: file creation, file deletion, or file modification. When the service detects an event of interest, it is forwarded to the registered process. The registered process has a thread or a pool of threads dedicated to watching for any events it has registered for. When an event comes in, it is handled as needed.

You can use it as is, or you can choose to create a high-level API on top of this mechanism so that it is suited to your particular needs. WatchKeys are thread-safe and can be used with the java. You can dedicate a thread pool to this effort. Because this API is more advanced, try it out before proceeding.

Save the WatchDir example to your computer, and compile it. Create a test directory that will be passed to the example. WatchDir uses a single thread to process all events, so it blocks keyboard input while waiting for events. Either run the program in a separate window, or in the background, as follows:.

Play with creating, deleting, and editing files in the test directory. When any of these events occurs, a message is printed to the console. When you have finished, delete the test directory and WatchDir exits.



0コメント

  • 1000 / 1000