Java实现文件夹下文件实时监控
一、commons-io方法
1、使用Commons-io的monitor下的相关类可以处理对文件进行监控,它采用的是观察者模式来实现的
- (1)可以监控文件夹的创建、删除和修改
- (2)可以监控文件的创建、删除和修改
- (3)采用的是观察者模式来实现的
- (4)采用线程去定时去刷新检测文件的变化情况
2、引入commons-io包,需要2.0以上。
|
1
2
3
4
5
6
|
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version></dependency> |
3、编写继承FileAlterationListenerAdaptor的类FileListener。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import java.io.File;import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;import org.apache.commons.io.monitor.FileAlterationObserver;import org.apache.log4j.Logger;/** * 文件变化监听器 * 在Apache的Commons-IO中有关于文件的监控功能的代码. 文件监控的原理如下: * 由文件监控类FileAlterationMonitor中的线程不停的扫描文件观察器FileAlterationObserver, * 如果有文件的变化,则根据相关的文件比较器,判断文件时新增,还是删除,还是更改。(默认为1000毫秒执行一次扫描) */public class FileListener extends FileAlterationListenerAdaptor { private Logger log = Logger.getLogger(FileListener.class); /** * 文件创建执行 */ public void onFileCreate(File file) { log.info("[新建]:" + file.getAbsolutePath()); } /** * 文件创建修改 */ public void onFileChange(File file) { log.info("[修改]:" + file.getAbsolutePath()); } /** * 文件删除 */ public void onFileDelete(File file) { log.info("[删除]:" + file.getAbsolutePath()); } /** * 目录创建 */ public void onDirectoryCreate(File directory) { log.info("[新建]:" + directory.getAbsolutePath()); } /** * 目录修改 */ public void onDirectoryChange(File directory) { log.info("[修改]:" + directory.getAbsolutePath()); } /** * 目录删除 */ public void onDirectoryDelete(File directory) { log.info("[删除]:" + directory.getAbsolutePath()); } public void onStart(FileAlterationObserver observer) { // TODO Auto-generated method stub super.onStart(observer); } public void onStop(FileAlterationObserver observer) { // TODO Auto-generated method stub super.onStop(observer); }} |
4、实现main方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static void main(String[] args) throws Exception{ // 监控目录 String rootDir = "D:\\apache-tomcat-7.0.78"; // 轮询间隔 5 秒 long interval = TimeUnit.SECONDS.toMillis(1); // 创建过滤器 IOFileFilter directories = FileFilterUtils.and( FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE); IOFileFilter files = FileFilterUtils.and( FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".txt")); IOFileFilter filter = FileFilterUtils.or(directories, files); // 使用过滤器 FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir), filter); //不使用过滤器 //FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir)); observer.addListener(new FileListener()); //创建文件变化监听器 FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer); // 开始监控 monitor.start(); } |
二、使用JDK7提供的WatchService
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public static void main(String[] a) { final Path path = Paths.get("D:\\apache-tomcat-7.0.78"); try (WatchService watchService = FileSystems.getDefault().newWatchService()) { //给path路径加上文件观察服务 path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); while (true) { final WatchKey key = watchService.take(); for (WatchEvent<?> watchEvent : key.pollEvents()) { final WatchEvent.Kind<?> kind = watchEvent.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) { continue; } //创建事件 if (kind == StandardWatchEventKinds.ENTRY_CREATE) { System.out.println("[新建]"); } //修改事件 if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { System.out.println("修改]"); } //删除事件 if (kind == StandardWatchEventKinds.ENTRY_DELETE) { System.out.println("[删除]"); } // get the filename for the event final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; final Path filename = watchEventPath.context(); // print it out System.out.println(kind + " -> " + filename); } boolean valid = key.reset(); if (!valid) { break; } } } catch (IOException | InterruptedException ex) { System.err.println(ex); } } |
三、以上方法都可以实现对相应文件夹得文件监控,但是在使用jdk7提供的API时,会出现些许问题。
- (1)当文件修改时,会被调用两次,即输出两个相同的修改。
- (2)不能对其子文件夹进行监控,只能提示目录被修改。
- (3)无法对文件类型进行过滤。
Java实现文件夹下文件实时监控的更多相关文章
- python调用另一个文件中的代码,pycharm环境下:同文件夹下文件(.py)之间的调用,出现红线问题
如何调用另一个python文件中的代码无论我们选择用何种语言进行程序设计时,都不可能只有一个文件(除了“hello world”),通常情况下,我们都需要在一个文件中调用另外一个文件的函数呀数据等等, ...
- Projects\Portal_Content\Indexer\CiFiles文件夹下文件占用磁盘空间过大问题。
C:\Program Files\Microsoft Office Servers\12.0\Data\Office Server\Applications\9765757d-15ee-432c-94 ...
- Linux统计某文件夹下文件、文件夹的个数
统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...
- Android Studio的使用(十)--读取assets、Raw文件夹下文件,以及menu、drawable文件夹
1.直接在/src/main目录下面新建assets目录 2.接下来即可读取文件 3.读取Raw文件夹下文件也类似.首先在res文件夹下新建raw目录,然后放入需要的文件即可读取. 4.menu和dr ...
- Linux上统计文件夹下文件个数以及目录个数
对于linux终端用户而言,统计文件夹下文件的多少是经常要做的操作,于我而言,我会经常在谷歌搜索一个命令,“如何在linux统计文件夹的个数”,然后点击自己想要的答案,但是有时候不知道统计文件夹命令运 ...
- Linux统计某文件夹下文件的个数
ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ...
- Linux 系统计算文件夹下文件数量数目
查看某目录下文件的个数(未包括子目录) ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l 查看某目录下文件的个数,包括 ...
- Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本
Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...
- Linux 统计文件夹下文件个数及目录个数
1. 统计文件夹下文件的个数 ls -l | grep "^-" | wc -l 2.统计文件夹下目录的个数 ls -l | grep "^d" | wc -l ...
- Linux随笔 - Linux统计某文件夹下文件、文件夹的个数
统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...
随机推荐
- PHP基础之排序
前言 之前简单介绍了流程控制,函数,数组等.有兴趣的可以看看. PHP入门之类型与运算符 PHP入门之流程控制 PHP入门之函数 PHP入门之数组 接下来介绍一下排序,排序是将一组数据,依指定的顺序进 ...
- 【Linux】linux history命令执行后显示历史命令执行时间
vim ~/.bashrc 或者 ~/.bash_profile 或者 获取root权限修改vim /etc/profile 最后添加 export HISTTIMEFORMAT="%F ...
- C#LeetCode刷题-Map
Map篇 # 题名 刷题 通过率 难度 846 一手顺子 33.7% 中等 855 考场就座 20.8% 中等
- SourceTreet提交时显示remote: Incorrect username or password ( access token )(4种解决办法)
引言 我因为第一次安装Sources Tree的时候进行破解时(跳过安装时的登录),因为操作失误造成了好多bug,导致Sources Tree不论提交,拉取,获取,都会报remote: Incorre ...
- Revit二次开发——非模态窗口的事件处理
一.起因 自己在写revit二开时,有一个Winform窗体按钮点击事件需要 触发调用事务进行处理,结果出现“异常“Starting a transaction from an external ...
- 41.4 Method Security方法安全性
41.4.1 <global-method-security> 这个元素是为Spring Security beans上的安全方法添加支持的主要手段.可以通过使用注释(在接口或类级别定义) ...
- Macbook pro 下修改MySQL数据库密码
步骤: 1. 打开终端 Terminal, 找到Mysql 的安装路径,一般默认安装路径为:/usr/local/mysql-5.7.12-osx10.11-x86_64/bin [注:我安装的Mys ...
- C# WebAPI项目,不支持HttpPut请求!!!
有点标题党了,这个异常的现象是这样的: 我有一个正在跑的项目,要对接第三方厂家的设备. 对方给的接口文档,里面有一个接口是需要我这边实现的,要求必须是PUT请求方式. 所以我在项目基础上,新增一个W ...
- python 04 分支和循环
创建一个框架 名字.py 打飞机游戏 加载音乐 播放音乐 我打飞机诞生 inter=0 while true: if 用户是否惦记了关闭: 退出程序 bleak inter+=1 if inter ...
- DP搬运工1 [来自yyy--mengbier的预设型dp]
DP搬运工1 题目描述 给你 \(n,K\) ,求有多少个 \(1\) 到 \(n\) 的排列,满足相邻两个数的 \(max\) 的和不超过 \(K\). 输入格式 一行两个整数 \(n,K\). 输 ...