java监听指定目录下文件、文件夹的创建、删除、改变
commons-io-2.0.1.jar下载地址:
http://repo1.maven.org/maven2/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar
代码部分
利用commons-io-2,5.jar实现对本地文件增删改的监听,代码如下:
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
public class FileAlterationListen extends FileAlterationListenerAdaptor {
public static String Reportname = "gsqxjb_risk_zyt_road";
public static String FileLocaltion = File.separator+"static"+File.separator+"pdf"+File.separator;
public File DirContext;
public FileAlterationListen(File dirContext) {
super();
DirContext = dirContext;
}
//文件夹创建
@Override
public void onDirectoryCreate(File directory) {
System.out.println(directory.getName()+" | 文件夹被创建"+" | 路径为:"+directory.getPath());
}
//文件夹改变
@Override
public void onDirectoryChange(File directory) {
System.out.println(directory.getName()+" | 文件夹被改变"+" | 路径为:"+directory.getPath());
}
//文件夹删除
@Override
public void onDirectoryDelete(File directory) {
System.out.println(directory.getName()+" | 文件夹被删除"+" | 路径为:"+directory.getPath());
}
//文件创建
@Override
public void onFileCreate(File file) {
super.onFileCreate(file);
System.out.println(file.getName() +" | 文件被创建"+" | 路径为:"+file.getPath());
traverseFolder2(DirContext.getPath(),file.getName());
}
//文件夹改变
@Override
public void onFileChange(File file) {
super.onFileChange(file);
System.out.println(file.getName() +" | 文件被修改"+" | 路径为:"+file.getPath());
traverseFolder2(DirContext.getPath(),file.getName());
}
//文件删除
@Override
public void onFileDelete(File file) {
super.onFileDelete(file);
System.out.println(file.getName()+" 文件被删除"+" 路径为:"+file.getPath());
}
public void traverseFolder2(String path,String fileName) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (files.length == ) {
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
traverseFolder2(file2.getAbsolutePath(),fileName);
} else {
if (fileName.equals(file2.getName())&&file2.getName().contains(Reportname)&&file2.getName().contains(".doc")) {
String name = file2.getName().substring(, file2.getName().length()-);
// 得到静态资源的相对地址
String Apath = FileAlterationListen.class.getClassLoader().getResource("").getPath().split("WEB-INF")[].replaceAll("/", "\\\\"),subPath=Apath.substring(, Apath.length());
File outFile = new File(subPath+FileLocaltion+name+".pdf");
// WordToPdf(file2, outFile);
}
}
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void main(String[] args) {
File dir = new File("D://监听/DSC");
FileAlterationMonitor monitor = new FileAlterationMonitor();
IOFileFilter filter = FileFilterUtils.or(FileFilterUtils.directoryFileFilter(),FileFilterUtils.fileFileFilter());
FileAlterationObserver observer = new FileAlterationObserver(dir,filter);
observer.addListener(new FileAlterationListen(dir));
monitor.addObserver(observer);
try {
//开始监听
monitor.start();
System.out.println("文件监听……");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 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
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" target="_blank" rel="external nofollow" rel="stylesheet">
</div>
</article>