如何监视文件夹中的java更改?

Hos*_*ein 7 java file-io multithreading file

我有以下代码用于监视java中任何更改的文件夹:

    public class FolderWatcher
    {
    // public List<Event> events = new ArrayList<Event>();

    public static Event call() throws IOException, InterruptedException
    {
        LOG.info("Watching folder");
        Path _directotyToWatch = Paths.get("data/input-files"); // this will be put in the configuration file
        WatchService watcherSvc = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = _directotyToWatch.register(watcherSvc,  ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

        watchKey = watcherSvc.take();
        for (WatchEvent<?> event : watchKey.pollEvents())
        {
            WatchEvent<Path> watchEvent = castEvent(event);
            LOG.info(event.kind().name().toString() + " " + _directotyToWatch.resolve(watchEvent.context()));
            String eventName = event.kind().name();
            String fileName = _directotyToWatch.resolve(watchEvent.context()).toString();
            watchKey.reset();
            return new Event(eventName, fileName);
        }
        return null;

    }

@SuppressWarnings("unchecked")
static <T> WatchEvent<T> castEvent(WatchEvent<?> event)
{
    return (WatchEvent<T>) event;
}

}
Run Code Online (Sandbox Code Playgroud)

和:

public abstract class AbstractWatcher
{
    abstract void eventDetected(Event event);

    private final ScheduledExecutorService threadpool;

    public AbstractWatcher(ScheduledExecutorService threadpool)
    {
        this.threadpool = threadpool;
    }

    public AbstractWatcher()
    {
        threadpool = Executors.newSingleThreadScheduledExecutor();
    }


    public void handle()
    {
        final FolderWatcherHandler handler = new FolderWatcherHandler();

        final Runnable r = new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    Event event = FolderWatcher.call();
                    if (event != null)
                    {
                        handler.eventDetected(event);
                    }
                }
                catch (IOException e)
                {
                        LOG.error("failed to watch the update", e);
                 }
                catch (InterruptedException e)
                {
                    LOG.info("thread interrupted", e);
                    Thread.currentThread().interrupt();
                    return;
                }

            }
        };

        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                    threadpool.shutdown();

            }
        });

        threadpool.scheduleWithFixedDelay(r, 0, 1, TimeUnit.NANOSECONDS);
    }
}
Run Code Online (Sandbox Code Playgroud)

和:

public class FolderWatcherHandler extends AbstractWatcher
{
    @Override
    public void eventDetected(Event event)
    {
         // Do stuff
        }
}
Run Code Online (Sandbox Code Playgroud)

只要文件的修改(在我的情况下大多是添加)是逐个完成的,并且在每次添加中有一个小的延迟,这整个过程都是完美的.但是,如果我拖放,或同时添加几个文件.此代码仅检测第一个文件的事件,而不检测其余文件.我甚至把执行时间都用在纳秒内,但它没有帮助.我想知道这整个代码是这样做的当前方式.有人能帮我吗.谢谢.

pra*_*abu 1

为什么不在metadata每个文件夹(recursively)中存储一个文件,在该metadata文件中您可以存储每个文件的file listmodified date和。size您的线程应该将metadata每个文件夹中的该文件与current files同一文件夹中的当前文件进行比较。这就是您检测该文件夹内任何更改的方法。

请记住,您应该对其中的每个内容递归地执行subfolder此操作。metadata每次扫描时都应更新该文件。希望这可以帮助..