0

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
Community
  • 1
  • 1
dimas
  • 2,487
  • 6
  • 40
  • 66
  • Probably you miss the right import for the class `StandardWatchEventKinds` as `import java.nio.file.StandardWatchEventKinds` – SubOptimal Feb 04 '15 at 12:53
  • yup did that and it would say this "The import java.nio.file.StandardWatchEventKind cannot be resolved" – dimas Feb 04 '15 at 13:00
  • The you must check if Eclipse is using a JDK < 7. Can be found for example under `help -> about eclipse -> installation details -> tab configuration` (the steps might be sligthly different on differen Eclipse versions) – SubOptimal Feb 04 '15 at 13:16

1 Answers1

0

A better solution would be to use a WatchEvent.

One extended example based on code from the Oracle tutorial can be found here https://github.com/jugsaar/jugsaar-meeting-9/blob/master/talks/java-nio2/src/main/java/de/jugsaar/meeting9/nio2/watchservice/WatchDir.java

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Thanks for Answering SubOptimal, I actually looked at that solution and others using WatchEvent. But I had a problem with StandardWatchEventKinds it cannot find that class. I am using Java 1.7 (jre 1.8.0_25) – dimas Feb 04 '15 at 12:23
  • @dimas The class is part of the API `StandardWatchEventKinds` [JDK 7](http://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardWatchEventKinds.html) / [JDK 8](http://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardWatchEventKinds.html). You say you are using Java 1.7, but mention a Java 1.8 JRE. What do you mean? – SubOptimal Feb 04 '15 at 12:27
  • sorry i meant java 1.8 – dimas Feb 04 '15 at 12:29
  • Verify your build/run environment. Or post the code you tried and an error message. – SubOptimal Feb 04 '15 at 12:37