使用线程监控文件目录变化

                    由于某种特殊的需求、弄了个使用线程监控文件目录变化的

代码基本如下、其中减去一些复杂的操作、只留下基本代码:

package com.file;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class FilesMonitor implements Runnable {
// 文件夹路径
private String filePath = "D:\\t\\user\\local\\test\\";
// 存放已读文件<即:缓存目录>
private static Map<String, File> map = new HashMap<String, File>();


@Override
public void run() {
while (true) {
try {
// 设置每隔3秒检测一次
Thread.sleep(3000);
FileMonitor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


// 文件监听
public void FileMonitor() {
File[] files = getFiles(filePath, null);
if (files != null && files.length > 0) {
// 如果缓存中文件与读取的个数不一样的时候
String fName = "";
if (files.length != map.size()) {
if (map.size() == 0) {
for (File file : files) {
fName = file.getName();
map.put(fName, file);
System.out.println("新增了文件:" + fName);
}
} else {
// 如果减少了文件
if (map.size() > files.length) {
List<String> removeName = new ArrayList<String>();
Iterator<String> iter = map.keySet().iterator();
int j = 0;
while (iter.hasNext()) {
String key = iter.next();
if (key != null && key.length() > 0) {
for (File file : files) {
fName = file.getName();
if (fName.equals(key)) {
j = 1;
break;
}
}
if (j != 1) {
removeName.add(key);
}
j = 0;
}
}
// 判断是否有删除的文件
if (removeName.size() > 0) {
for (String item : removeName) {
map.remove(item);
System.out.println("减少了文件:" + item);
}
}
} else {
for (File file : files) {
fName = file.getName();
if (!map.containsKey(fName.trim())) {
map.put(fName, file);
System.out.println("新增了文件:" + fName);
}
}
}
}
} else {
map.clear();
for (File file : files) {
fName = file.getName();
map.put(fName, file);
}
}
System.out.println("此时缓存中文件个数:" + map.size());
}
}


/**
* 文件读取

* @param filePath
*            路径
* @param fileName
*            名称
* @return 返回文件数组
*/
public File[] getFiles(String filePath, String fileName) {
File[] files = null;
if (fileName == null) {
File doc = new File(filePath);
if (doc.isDirectory()) {
String[] fileNameArr = doc.list();
if (fileNameArr.length > 0) {
files = new File[fileNameArr.length];
for (int i = 0; i < fileNameArr.length; i++) {
fileName = fileNameArr[i];
String fileAbsPath = filePath + fileName;
File regInfoFile = new File(fileAbsPath);
files[i] = regInfoFile;
}
}
}
} else {
String path = filePath + fileName;
File doc = new File(path);
if (doc.isFile()) {
files = new File[1];
files[0] = doc;
}
}
return files;
}


// 启动线程
public void show() {
FilesMonitor t = new FilesMonitor();
Thread tread = new Thread(t);
tread.setName("eshore");
tread.start();
}


// Main测试
public static void main(String[] args) {
FilesMonitor t = new FilesMonitor();
t.show();
}
}

执行后,效果图如下:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值