Thursday 10 September 2015

Java 7 - NIO - Watcher implementation

One of the new features of Java7 was NIO package. 
Java NIO also known as new input output library as is a set of classes defined under package java.nio.* to access files, file attributes and file systems.

The main classes from this package are:
  • Path - to manage file location and directory hierarchy
  • Files - to manage file operations
Some functionalities that can be implemented are:
  • Walking the file tree
  • Watchers - monitoring the creation/modification/deletion of files
  • Copy/Create/Delete files from a directory(atomic operations with synchronization)
  • Find files using matcher expressions
In the following example we are going to see how to implement a watcher. This is useful when we need a process to monitor when a file is created/modified or deleted in a directory.

Steps in this example:
1- watch for new files in a directory
2- evaluate the mime type of the file
3- if it is a text file it will be copied to other directory.


No comments:

Post a Comment