File JDirMonitor listener Java API (updated) A few days ago there was a problem in a system and part of the solution proposed involved read written information in real time from multiple files of bytes serialized objects, process and delegating them to a annex, all in Java.
The first thing I thought was "googling" to find an API to do the job of monitoring changes to files in a directory and surprise I found that did not exist.
Once solved the problem I was interested in the idea of \u200b\u200bcreating a sufficiently generic API, configurable and easy to use that to solve that 'problem'.
And so was born an API GetSoftware JDirMonitor written by me. Then
JDirMonitor is an API, LGPL licensed that allows monitoring practice and 'hear' the changes that have all the files that are within a defined directory, warning failing which have been created, modified and deleted. In addition
JDirMonitor has the following features:-Define multiple
listeners to capture events occurring over the files. Definition of Filters
to be applied to files, for example, distinguish those that must be processed. Comparators
Definition of File objects, for example, define the order in which they must process each file.
failing JDirMonitor offers 2 types of monitoring:
1) NormalTaskMonitor: Scans only files found on the first level of the specified directory.
2) RecursiveTaskMonitor: scans all files located within the directory to scan, recursive IN ORDER, as accessing the files in all subdirectories (at any depth).
The following example code allows you to monitor a directory NormalTaskMonitor X, a listener and a file filter:
import java.io.File;
java.util.concurrent.TimeUnit import, import
cl. getsoftware.jdirmonitor.DirectoryMonitor;
import cl.getsoftware.jdirmonitor.MonitorBuilder;
import cl.getsoftware.jdirmonitor.exception.DirectoryMonitorException;
import cl.getsoftware.jdirmonitor.filter.RegexNameFileFilter;
import cl.getsoftware.jdirmonitor.listener.FileListener;
import cl.getsoftware.jdirmonitor.task.NormalTaskMonitor;
public class Test {
public static void main(String[] args) throws DirectoryMonitorException {
pathDirectory String = "ruta / del / directorio / a / monitorear";
long timeTaskExecute = 2;
/ / Clase ; que añadir archivos y permite a Filtros directorios
/ / mediante Expresiones Regulares
RegexNameFileFilter regexName =
; RegexNameFileFilter new ();
regexName.addFileNameRegex ( ? [a-zA-Z] .* \\ \\ $ txt. ");
MonitorBuilder builder = new MonitorBuilder(pathDirectory,
timeTaskExecute, new NormalTaskMonitor(),
TimeUnit.SECONDS);
builder.addListener(fileListener);
builder.setFileFilter( regexName );
DirectoryMonitor monitor = builder.build();
monitor.start();
/ / if it Deja hilo main live in an endless cycle
/ / to see what the listeners are printed on the console
while (true) {
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {}
}
}
public static FileListener fileListener = new FileListener() {
@Override
public void fileDeleted(File file) {
System.out.println ("Archivo" file.getAbsolutePath + () + "ha sido BORRADO");}
@ Override
void created file (File file) {
System.out.println ("Archivo" file.getAbsolutePath + () + "ha sido creado");
;}
@ Override
public void changed file (File file) {
System.out.println ("Archivo , "+ file.getAbsolutePath () +" ha sido modificado ");
}};}
Quite simply, we have 4 lines running in a directory DirectoryMonitor to review periodically each N seconds, will inform us the listener through the changes that occur in the file and class cl.getsoftware.jdirmonitor.filter.RegexNameFileFilter filter is achieved only files that have a name that complies with the regular expression defined.
Another advantage is that the API allows you to encode your own kinds of monitoring, which should inherit from cl.getsoftware.jdirmonitor.AbstractTaskMonitor.
For example, this is the class source code cl.getsoftware.jdirmonitor.task.NormalTaskMonitor:
/ * * JDirMonitor
a listener of files
* Copyright (C) 2010 GetSoftware Group
* * This library is free software; You Can redistribute it and / or
* modify it under the Terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* This library was written by Eugenio Contreras for GetSoftware Group
* Contact email: contacto@getsoftare.cl
* / package
cl.getsoftware.jdirmonitor.task;
import java.io.File;
import cl. getsoftware.jdirmonitor.AbstractTaskMonitor;
/ ** * \u0026lt;p>
Tarea de monitoreo de Directorio que analiza los archivos que solamente se
* ; encuentran en el primer nivel del Directorio especificado. \u0026lt;/ p>
*
* @author egacl
*
*/
public class NormalTaskMonitor extends AbstractTaskMonitor {
/** (non-Javadoc)
* @see cl.getsoftware.jdirmonitor.AbstractTaskMonitor#monitorRutine(java.io.File)
*/
@Override
public void monitorRutine(File directory) {
this.normalRutine( this.getFilesOfDirectory(directory) );
}
public void normalRutine( File []files ){
for( File file : files ){
if( file.exists() ){
if( file.isFile() ){
this.fileMonitor( file );
}
}
}}}
monitorRutine The method is executed each time the DirectoryMonitor routine monitoring starts and sends the object as a parameter to analyze java.io.File directory. So there you write the way you want to do the monitoring.
can download the file and JDirMonitor.jar JDirMonitor_Javadoc from the google group of getgeek .