在Java中跟踪文件夹更改的最简单方法是什么? [英] What is the easiest way to track a change in a folder in Java?

查看:134
本文介绍了在Java中跟踪文件夹更改的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组源文件夹。我使用Java类从这些文件夹中构建分发文件。我想在Java中编写另一个小类,每半秒运行一次,检查文件夹中的任何文件是否已更改,如果是,则运行构建类。

I have a set of source folders. I use a Java class to build the distribution file out of these folders. I'd like to write another little class in Java which runs every half a second, checks if any of the files in the folders have changed, and if yes, run the building class.

那么,如何轻松检测到文件夹已被修改?

So, how do I detect easily that a folder has been modified ?

推荐答案

我认为您需要检查目录和子目录修改时间(对于要添加/删除的文件)和文件修改时间(对于每个文件中的更改)。

I think you will need to check the directory and subdirectory modfication times (for files being added/removed) and the file modification times (for the changes in each file).

编写一个检查a的递归例程目录为它的修改时间,如果它被更改,加上每个文件。然后检查目录内容,并递归调用任何子目录。您应该能够检查比上次检查时更长的修改时间。

Write a recursive routine that checks a directory for it's modification time and if it's changed, plus each files. Then checks the directory contents, and call recursively for any subdirectories. You should just be able to check for any modification times greater than when you last ran the check.

参见 File.lastModified()

编辑:自从我上面写了以后,Java 7推出了它的目录观看功能

Since I wrote the above, Java 7 came out with its directory watching capability.

这篇关于在Java中跟踪文件夹更改的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆