Home  >  Article  >  Java  >  Java uses threads to implement methods for monitoring changes in file directories

Java uses threads to implement methods for monitoring changes in file directories

黄舟
黄舟Original
2017-10-19 09:55:511942browse

这篇文章主要介绍了java 使用线程监控文件目录变化的实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

java 使用线程监控文件目录变化的实现方法

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

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


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();
}
}

执行后,效果图如下:

The above is the detailed content of Java uses threads to implement methods for monitoring changes in file directories. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn