Hi guys this is somewhat a reference to my previous question here. My update on that post was working but not all the time. So i made a few changes see my code below by introducing a HashSet but still its not 100% working. I scan a file every 3 secs and it can detect changes maybe 60-70% of the time there is a file modification. Can anyone help out please. Thanks
public static long checkLastFileModification() throws InterruptedException{
File file = new File(fileName);
long lastProcessed = -1;
while (true){
long lastModified = file.lastModified();
if (lastProcessed != lastModified){
lastProcessed = lastModified;
//If hashset doesn't contain lastprocessed value readFileInReverse
if ( !(listOfUniqueModNum.contains(lastProcessed)) ){
//read the file in reverse order
System.out.println("Send a File");
listOfUniqueModNum.add(lastProcessed);
}
}
else{
System.out.println("No file access.... next scan in 3 secs ");
Thread.sleep(3000);
}
}
}
Update
code taken from this link
public void detectFileChanges(){
//define a folder root
Path myDir = Paths.get("D:/data");
try {
WatchService watcher = myDir.getFileSystem().newWatchService();
myDir.register(watcher, StandardWatchEventKind.ENTRY_CREATE,
StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY);
WatchKey watckKey = watcher.take();
List<WatchEvent<?>> events = watckKey.pollEvents();
for (WatchEvent event : events) {
if (event.kind() == StandardWatchEventKind.ENTRY_CREATE) {
System.out.println("Created: " + event.context().toString());
}
if (event.kind() == StandardWatchEventKind.ENTRY_DELETE) {
System.out.println("Delete: " + event.context().toString());
}
if (event.kind() == StandardWatchEventKind.ENTRY_MODIFY) {
System.out.println("Modify: " + event.context().toString());
}
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
Error : StandardWatchEventKind cannot be resolved to a variable
I've pointed my eclipse build path to use jdk 1.7.0_75
Eclipse configuration
org.eclipse.epp.package.java.product
-vm
C:\Program Files\Java\jre1.8.0_25\bin\server\jvm.dll
eclipse.stateSaveDelayInterval=30000
eclipse.vm=C:\Program Files\Java\jre1.8.0_25\bin\server\jvm.dll
eclipse.vmargs=-Dosgi.requiredJavaVersion=1.6
-Xms40m
-Xmx512m
-XX:MaxPermSize=256m
-Djava.class.path=C:\Users\ervin \Documents\O\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
equinox.use.ds=true
Files\Java\jre1.8.0_25\lib\ext;C:\windows\Sun\Java\lib\ext
java.home=C:\Program Files\Java\jre1.8.0_25
java.io.tmpdir=C:\Users\ERVINV~1\AppData\Local\Temp\
java.library.path=C:\Users\ervin vinluan\Documents\O\eclipse;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_25/bin/server;C:/Program Files/Java/jre1.8.0_25/bin;C:/Program Files/Java/jre1.8.0_25/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files (x86)\Autodesk\Backburner\;C:\Users\ervin vinluan\Documents\O\eclipse;;.
java.runtime.name=Java(TM) SE Runtime Environment
java.runtime.version=1.8.0_25-b18
java.specification.name=Java Platform API Specification
java.specification.vendor=Oracle Corporation
java.specification.version=1.8
java.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
java.version=1.8.0_25
java.vm.info=mixed mode
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.vm.specification.name=Java Virtual Machine Specification