Ho un'applicazione in cui voglio ascoltare le modifiche apportate a una directory specifica. L'applicazione dovrebbe eseguire il ping non appena ci sono file aggiunti, cancellati o aggiornati in quella directory.Listener di directory in Java
risposta
È possibile utilizzare JNotify
JNotify è una libreria Java che permettono di applicazioni Java per ascoltare un file eventi di sistema, come ad esempio: file creato File file modificato rinominato File cancellato supportati piattaforme
Windows (2000 o più recente) Note di Windows Linux con supporto INofity (2.6.14 o più recenti) Note Linux Mac OS X (1 0.5 o più recente ) Mac OS nota
Ulteriori informazioni:
Scarica JNotify da here
estrarre la zip, inserire .dll/.so in base alla piattaforma nel tuo percorso lib. e creare una classe fornire jnotify-0.93.jar
nel percorso di classe.
codice di esempio:
package org.life.java.stackoverflow.questions;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
/**
*
* @author Jigar
*/
public class JNotifyDemo {
public void sample() throws Exception {
// path to watch
String path = System.getProperty("user.home");
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED
| JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED
| JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
public static void main(String[] args) throws Exception {
new JNotifyDemo().sample();
}
}
uscita:
modified C:\Documents and Settings\jigar: LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea9
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Current Session
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea8
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
Jnotify per notifica file in java. Esempio di codice
public void sample() throws Exception {
// path to watch
String path = System.getProperty("user.home");
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// watch subtree? boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener
{
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName); }
public void fileModified(int wd, String rootPath, String name)
{ print("modified " + rootPath + " : " + name); }
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name); }
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name); }
void print(String msg) {
System.err.println(msg); }
}
quando ho eseguito il programma mi dà biblioteca carico exceptionError, java.library.path = C: \ Program Files \ Java \ jdk1.7.0 \ bin;; C: \ Windows. \ Sun \ Java \ bin; C: \ Windows \ system32; C: \ Windows, C: \ Windows, C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; C: \ Programmi \ Microsoft SQL Server \ 90 \ Tools \ binn \; C: \ apache-tomcat-6.0.26 \ bin; C: \ Programmi \ Java \ jdk1 .7.0 \ bin; C: \ Programmi \ TortoiseSVN \ bin; C: \ Programmi \ putty; C: \ Programmi \ Google \ Chrome \ Applicazione; C: \ Programmi \ Java \ jdk1.7.0 \ include Eccezione nella discussione "main" java.lang.Unsa – Jinith
1.- Fare clic con il tasto destro sul progetto 2.- Proprietà 3.- Fare clic su RUN 4.- Opzioni VM: java -Djava.library.path = "your_path" 5.- ad esempio nel mio caso: java -Djava.library.path = Users/Lexynux/NetBeansProjects/NAO/libs> 6.- Ok –
Dal momento che Java 1.7 è possibile utilizzare la Watch Service API per registrarsi per gli eventi di directory. Fa parte della libreria Java New I/O (NIO) e non richiede risorse aggiuntive. Un esempio di come utilizzare l'API può essere trovato nel official documentation.
Dopo aver registrato il WatchService è possibile recuperare gli eventi per il percorso di destinazione in questo modo:
for (WatchEvent<?> event: key.pollEvents()) {
// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
// print out event
System.out.format("%s: %s\n", event.kind().name(), child);
}
* Downvoters * per favore commenta –
Quando eseguo il programma mi dà una libreria di caricamento exceptionError, java.library.path = C : \ Programmi \ Java \ jdk1.7.0 \ bin;;; C: \ Windows \ Sun \ Java \ bin; C: \ Windows \ system32; C: \ Windows; C: \ Windows; C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; C: \ Programmi \ Microsoft SQL Server \ 90 \ Tools \ binn \; C: \ apache-tomcat-6.0.26 \ bin; C: \ Programmi \ Java \ jdk1.7.0 \ bin; C: \ Programmi \ TortoiseSVN \ bin; C: \ Programmi \ putty; C: \ Programmi \ Google \ Chrome \ Applicazione; C: \ Programmi \ Java \ jdk1.7.0 \ include Eccezione nella discussione "main" java.lang.Unsa – Jinith
'Estrai il file zip, metti .dll/.so secondo la piattaforma nel tuo path lib. –