search
HomeJavajavaTutorialJava uses threads to implement methods for monitoring changes in file directories

这篇文章主要介绍了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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools