Hallo zusammen,
ich möchte gerne einen FileWatcher einsetzen, was auch soweit tut.
Sobald ein neues File in den angegebenen Ordner kommt, bekomme ich eine Rückmeldung.
Jetzt ist aber das Problem, dass sofort wenn eine Datei in den Ordner kommt, eine Rückmeldung kommt.
Heißt wenn ich ein z.B. 600 MB großes File habe, dass dann das File u.U. noch gar nicht komplett in den Ordner kopiert wurde, aber ich schon eine Rückmeldung habe, dass ein neues File gekommen ist.
Ich möchte also die Rückmeldung erst, wenn das File komplett kopiert wurde:
Hier mal der Code:
ich möchte gerne einen FileWatcher einsetzen, was auch soweit tut.
Sobald ein neues File in den angegebenen Ordner kommt, bekomme ich eine Rückmeldung.
Jetzt ist aber das Problem, dass sofort wenn eine Datei in den Ordner kommt, eine Rückmeldung kommt.
Heißt wenn ich ein z.B. 600 MB großes File habe, dass dann das File u.U. noch gar nicht komplett in den Ordner kopiert wurde, aber ich schon eine Rückmeldung habe, dass ein neues File gekommen ist.
Ich möchte also die Rückmeldung erst, wenn das File komplett kopiert wurde:
Hier mal der Code:
Code:
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class MainWatch {
public static void watchDirectoryPath(Path path) {
// Sanity check - Check if path is a folder
try {
Boolean isFolder = (Boolean) Files.getAttribute(path,
"basic:isDirectory", NOFOLLOW_LINKS);
if (!isFolder) {
throw new IllegalArgumentException("Path: " + path + " is not a folder");
}
} catch (IOException ioe) {
// Folder does not exists
ioe.printStackTrace();
}
System.out.println("Watching path: " + path);
// We obtain the file system of the Path
FileSystem fs = path.getFileSystem ();
// We create the new WatchService using the new try() block
try(WatchService service = fs.newWatchService()) {
// We register the path to the service
// We watch for creation events
path.register(service, ENTRY_CREATE);
// Start the infinite polling loop
WatchKey key = null;
while(true) {
key = service.take();
// Dequeueing events
Kind<?> kind = null;
for(WatchEvent<?> watchEvent : key.pollEvents()) {
// Get the type of the event
kind = watchEvent.kind();
if (OVERFLOW == kind) {
continue; //loop
} else if (ENTRY_CREATE == kind) {
// A new Path was created
Path newPath = ((WatchEvent<Path>) watchEvent).context();
// Output
System.out.println("New path created: " + newPath);
}
}
if(!key.reset()) {
break; //loop
}
}
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
public static void main(String[] args) throws IOException,
InterruptedException {
// Folder we are going to watch
Path folder = Paths.get(System.getProperty("user.home"));
watchDirectoryPath(folder);
}
}