这里有新鲜出炉的Java设计模式,程序狗速度看过来!
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要介绍了Java实现文件变化监控的实现代码,代码附有注释,分步骤介绍的非常详细,非常不错,具有参考借鉴价值,,需要的朋友可以参考下
一. spring配置文件:
application.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
- xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
- http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
- <!-- 文件监测器 -->
- <bean id="monitor" class="com.interfaces.file.monitor.FileMonitorImpl">
- <constructor-arg index="0" value="10000" />
- <!-- 监测时间间隔,单位:毫秒 -->
- <constructor-arg index="1" ref="observer" />
- <!-- 文件观察器 -->
- </bean>
- <!-- 文件观察器 -->
- <bean id="observer" class="com.interfaces.file.monitor.FileObserverImpl">
- <constructor-arg index="0" value="D:\\UploadDir" />
- <!-- 观察的目录 -->
- <constructor-arg index="1" ref="filter" />
- <!-- 文件过滤器-->
- <constructor-arg index="2" ref="listener" />
- <!-- 文件监听器 -->
- </bean>
- <!-- 文件监听器 -->
- <bean id="listener" class="com.interfaces.file.monitor.FileListener" />
- <!-- 文件过滤器 -->
- <bean id="filter" class="com.interfaces.file.monitor.FileFilterImpl">
- <!-- 指定文件扩展名,只有指定的扩展名文件会被处理。 不同的扩展名间以 "," 间隔,如:xml,txt,bak -->
- <constructor-arg index="0" value="xml" />
- </bean>
- </beans>
二. spring上下文加载监听器:
SpringContextLoaderListener.class
- import javax.servlet.ServletContextEvent;
- import org.springframework.web.context.ContextLoaderListener;
- public class SpringContextLoaderListener extends ContextLoaderListener{
- @Override
- public void contextInitialized(ServletContextEvent event) {
- super.contextInitialized(event);
- FileMonitor scanner = getScanner();
- // 启动目录扫描器
- scanner.start();
- }
- @Override
- public void contextDestroyed(ServletContextEvent event) {
- FileMonitor scanner = getScanner();
- // 关闭目录扫描器
- scanner.stop();
- super.contextDestroyed(event);
- }
- /**
- * 获取目录扫描器
- * @return
- */
- private FileMonitor getScanner() {
- return getCurrentWebApplicationContext().getBean(FileMonitor.class);
- }
- }
三. web工程配置文件:
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:application.xml
- </param-value>
- </context-param>
- <listener>
- <listener-class>com.web.SpringContextLoaderListener</listener-class>
- </listener>
- </web-app>
四. 文件监测器
1. 接口:FileMonitor.class
- import org.apache.commons.io.monitor.FileAlterationObserver;
- /**
- * 文件监测器角色
- */
- public interface FileMonitor {
- /**
- * 注册观察器
- * @param observer 观察器
- */
- void addObserver(FileAlterationObserver observer);
- /**
- * 删除观察器
- * @param observer 观察器
- */
- void removeObserver(FileAlterationObserver observer);
- /**
- * 获取注册的所有观察器
- * @return 观察器集合
- */
- Iterable < FileAlterationObserver > getObservers();
- /**
- * 启动监测器
- */
- void start();
- /**
- * 停止监测器
- */
- void stop();
- /**
- * 获取监测间隔时间
- * @return 间隔时间(单位:毫秒)
- */
- long getInterval();
- }
2. 实现类:FileMonitorImpl.class
- import java.util.concurrent.ThreadFactory;
- import org.apache.commons.io.monitor.FileAlterationMonitor;
- import org.apache.commons.io.monitor.FileAlterationObserver;
- import org.apache.commons.lang3.concurrent.BasicThreadFactory;
- /**
- * 监测器,监测时间间隔,设置文件观察器
- */
- public class FileMonitorImpl implements FileMonitor{
- private final FileAlterationMonitor monitor;
- /**
- * 监测器线程名称
- */
- private static final String MONITOR_THREAD_NAME = "File MONITOR Daemon";
- /**
- * 监测器线程Daemon标记
- */
- private static final boolean DAEMON = false;
- /**
- * 定义监测时间间隔、文件观察器
- * @param interval 监测时间间隔
- * @param observer 文件观察者
- */
- FileMonitorImpl(int interval, final FileAlterationObserver observer) {
- this(interval, observer,
- new BasicThreadFactory.Builder().
- namingPattern(MONITOR_THREAD_NAME).daemon(DAEMON).build());
- }
- /**
- * 定义监测时间间隔、文件观察器和线程工厂
- * @param interval 监测时间间隔
- * @param observer 文件观察器
- * @param factory 线程工厂
- */
- FileMonitorImpl(int interval, final FileAlterationObserver observer,
- final ThreadFactory factory) {
- this.monitor = new FileAlterationMonitor(interval, new FileAlterationObserver[] { observer });
- monitor.setThreadFactory(factory);
- }
- /**
- * 添加文件观察器
- * @param observer
- */
- @Override
- public void addObserver(FileAlterationObserver observer) {
- monitor.addObserver(observer);
- }
- /**
- * 删除文件观察器
- * @param observer
- */
- @Override
- public void removeObserver(FileAlterationObserver observer) {
- monitor.removeObserver(observer);
- }
- /**
- * 获取注册的所有观察器
- * @return
- */
- @Override
- public Iterable<FileAlterationObserver> getObservers() {
- return monitor.getObservers();
- }
- /**
- * 启动监测器
- */
- @Override
- public void start() {
- try {
- monitor.start();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 停止监测器
- */
- @Override
- public void stop() {
- try {
- monitor.stop();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 获取监测时间间隔
- */
- @Override
- public long getInterval() {
- return monitor.getInterval();
- }
- }
五. 文件观察器
1. 接口:FileObserver.class
- import java.io.File;
- import org.apache.commons.io.monitor.FileAlterationListener;
- /**
- * 文件观察器角色
- */
- public interface FileObserver {
- /**
- * 添加监听器
- * @param listener
- */
- void addListener(final FileAlterationListener listener);
- /**
- * 删除监听器
- * @param listener
- */
- void removeListener(final FileAlterationListener listener);
- /**
- * 获取注册的监听器
- * @return
- */
- Iterable < FileAlterationListener > getListeners();
- /**
- * 初始化观察器
- * @throws Exception
- */
- void initialize() throws Exception;
- /**
- * 销毁观察器
- * @throws Exception
- */
- void destroy() throws Exception;
- /**
- * 获取观察的目录
- * @return
- */
- File getDirectory();
- /**
- * 获取文件过滤器
- *
- * @return
- */
- public FileFilter getFilter();
- }
2. 实现类:FileObserverImpl.class
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOCase;
- import org.apache.commons.io.monitor.FileAlterationListener;
- import org.apache.commons.io.monitor.FileAlterationObserver;
- /**
- * 文件观察器
- *
- * 当有文件创建、删除、或变更动作时,则消息通知监听器
- */
- public class FileObserverImpl extends FileAlterationObserver implements FileObserver{
- private static final long serialVersionUID = -7239227289538993830L;
- /**
- * 文件过滤器
- */
- private final FileFilter filter;
- /**
- * 设置要监听观察的目录,并设置文件过滤器和监听器,用以观察指定具有指定扩展名的文件
- * @param dir 观察监听的目录
- * @param filter 文件过滤器
- * @param listener 文件监听器
- */
- public FileObserverImpl(String dir, final FileFilter filter,
- FileAlterationListener listener) {
- super(dir, filter, (IOCase) null);
- addListener(listener);
- this.filter = filter;
- File directory = new File(dir);
- // 如果目录不存在
- if(!directory.exists()) {
- try {
- FileUtils.forceMkdir(directory);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 如果存在的是文件
- else if(directory.exists() && directory.isFile()) {
- try {
- FileUtils.forceDelete(directory);
- FileUtils.forceMkdir(directory);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 添加监听器
- */
- @Override
- public void addListener(final FileAlterationListener listener) {
- super.addListener(listener);
- }
- /**
- * 移除监听器
- */
- @Override
- public void removeListener(final FileAlterationListener listener) {
- super.removeListener(listener);
- }
- /**
- * 获取观察者对象的所有监听器
- */
- @Override
- public Iterable<FileAlterationListener> getListeners() {
- return super.getListeners();
- }
- /**
- * 初始化文件观察者
- */
- @Override
- public void initialize() throws Exception {
- super.initialize();
- }
- /**
- * 销毁文件观察者
- */
- @Override
- public void destroy() throws Exception {
- super.destroy();
- }
- /**
- * 获取所观察的目录
- */
- @Override
- public File getDirectory() {
- return super.getDirectory();
- }
- /**
- * 获取文件过滤器
- * @return
- */
- public FileFilter getFilter() {
- return filter;
- }
- }
六. 文件监听器:
FileListener.class
- import java.io.File;
- import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
- import org.apache.commons.io.monitor.FileAlterationObserver;
- /**
- * 文件监听器
- */
- public final class FileListener extends FileAlterationListenerAdaptor {
- /**
- * 文件创建时执行的动作
- */
- @Override
- public void onFileCreate(File file) {
- // To do something
- }
- /**
- * 文件删除(转移)时执行的动作
- */
- @Override
- public void onFileDelete(File file) {
- // To do something
- }
- /**
- * 文件内容改变时执行的动作
- */
- @Override
- public void onFileChange(File file) {
- // To do something
- }
- /**
- * 开始执行监听时执行的动作
- */
- @Override
- public void onStart(FileAlterationObserver observer) {
- // To do something
- }
- /**
- * 停止监听时执行的动作
- */
- @Override
- public void onStop(FileAlterationObserver observer) {
- // To do something
- }
- }
七. 文件过滤器
1. 接口:FileFilter.class
- /**
- * 文件过滤器角色,扩展自java.io.FileFilter
- */
- public interface FileFilter extends java.io.FileFilter {
- /**
- * 获取定义的扩展名
- *
- * @return
- */
- String[] getExtensions();
- }
2. 实现类:
FileFilterImpl.class
- import java.io.File;
- import org.apache.commons.io.FilenameUtils;
- /**
- * 文件过滤器
- */
- public class FileFilterImpl implements FileFilter {
- private String[] extensions;
- public FileFilterImpl(String...extensions) {
- this.extensions = extensions;
- }
- /**
- * 是否接受该文件
- */
- @Override public boolean accept(File pathname) {
- return FilenameUtils.isExtension(pathname.getName(), extensions);
- }
- /**
- * 获取定义的扩展名
- * @return
- */
- @Override public String[] getExtensions() {
- return extensions;
- }
- }
以上所述是小编给大家介绍的Java实现文件变化监控,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!
来源: http://www.phperz.com/article/17/1110/360206.html