非常に小さなファイルをコピーして、監視サービスによって監視されるフォルダーに貼り付けようとしました。初めてはうまくいきますが、その後のすべてのコピー&ペースト操作で、別のプロセスがすでにファイルを処理しているという例外が発生します。実験の結果、コンテンツがコピーされたときではなく、Windows がファイルを作成したときにサービスに通知されることがわかりました。ファイルをロックすると、Windows はデータをコピーできず、ファイルは空になります。一方、ファイルをディレクトリに移動すると、すべて正常に動作します。
それはWindowsのバグですか?Mac または Linux ワークステーションではテストできませんでした。あるいは、私が無能だっただけかもしれません。どんな助けでも大歓迎です。
私のコードは次のようになります。
try (WatchService watchService = importPath.getFileSystem().newWatchService()) {
importPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
handleExistingFiles();
try {
do {
WatchKey watchKey = watchService.take();
if (!watchKey.isValid()) {
continue;
}
boolean hasCreationEvents = false;
for (WatchEvent<?> event : watchKey.pollEvents()) {
hasCreationEvents |= event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE);
}
watchKey.reset();
if (hasCreationEvents) {
handleNewFiles();
}
}
while (!Thread.currentThread().isInterrupted());
}
catch (InterruptedException ignoredEx) {
Thread.currentThread().interrupt();
}
}