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

2025-05-29 0 53

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

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

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

?

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

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

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

}

}

执行后,效果图如下:

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

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/vtopqx/article/details/8264236

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 使用线程监控文件目录变化的实现方法 https://www.kuaiidc.com/114277.html

相关文章

发表评论
暂无评论