网站首页
网站导航
Ctrl+D收藏
首 页
代码段
源码包
文档库
工具箱
代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
JS
】
实现服务器数据同步,并实时监听文件动态
作者:
邓诚
/ 发布于
2013/9/2
/
580
package com.honest.listener; import org.apache.log4j.Logger; import com.honest.listener.FileUtil.PropertiesUtil; import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyException; public class FielListener extends Thread { private static Logger logger = Logger.getLogger(FielListener.class); public void run() { logger.info("文件监听器已启动"); PropertiesUtil config=FileUtil.PropertiesUtil.getInstance(); while (true) { int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; //参数FILE_CREATED 创建 FILE_DELETED 删除 FILE_RENAMED 重命名 FILE_MODIFIED文件变动 try { int watchID=JNotify.addWatch(config.getListenerPath(), mask, true, new ActivityFileListener());//执行监听 long sleep=config.getSyncTime(); sleep(sleep);//默认每分钟监听一次 JNotify.removeWatch(watchID); } catch (JNotifyException e) { logger.error(e); } catch (InterruptedException e) { logger.error(e); } } } public static void main(String[] args) { new FielListener().start(); } } /** * * @Description : 新建文件 * @param rootPath * @param name * void Created by [Aug 30, 2013] [2:14:38 PM] */ public static void createFile(String rootPath, String name) { renamedFile(rootPath,name,name); } /** * * @Description : 删除文件 * @param rootPath * @param name * void Created by [Aug 30, 2013] [2:14:54 PM] */ public static void deleteFile(String rootPath, String name) { File tempFile = getFile(rootPath, name); boolean isprocess = isBeingUsed(tempFile); if (isprocess) {// 文件被占用 deleteFile(rootPath, name); } else { try { Runtime.getRuntime().exec("rd/s/q "+tempFile.getPath());//这样删除文件避免无法删除非空目录 } catch (IOException e) { logger.error(e); } } } /** * * @Description : 文件重命名 * @param rootPath * @param oldName * @param newName * void Created by [Aug 30, 2013] [2:14:57 PM] */ public static void renamedFile(String rootPath, String oldName, String newName) { try { String tempname = PropertiesUtil.getInstance().getListenerPath() + File.separator + newName; File tempFile = getFile(rootPath, oldName); boolean isprocess = isBeingUsed(tempFile); if (isprocess) { renamedFile(rootPath, oldName, newName); } else { tempFile.renameTo(new File(tempname)); } } catch (Exception e) { logger.error(e); } } /** * * @Description : 文件改动 * @param rootPath * @param name * void Created by [Aug 30, 2013] [2:15:02 PM] */ public static void modifiedFile(String rootPath, String name) { deleteFile(rootPath, name); createFile(rootPath, name); } /** * * @Description : 文件是否被占用 * @param file * @return boolean Created by [Aug 30, 2013] [2:23:17 PM] */ public static boolean isBeingUsed(File file) { return (file.exists() && !file.renameTo(file)); } /** * * @Description : 获取同步需要同步文件的路径 * @param rootPath * @param name * @return String Created by [Aug 30, 2013] [2:37:27 PM] */ private static File getFile(String rootPath, String name) { String path = ""; String oldPath = rootPath + File.separator + name; try { String syncPath = PropertiesUtil.getInstance().getSyncPath(); oldPath = oldPath.replace(syncPath, ""); String listenerPath = PropertiesUtil.getInstance() .getListenerPath(); path = listenerPath + oldPath; } catch (Exception e) { logger.error(e); } return new File(path); } public static class PropertiesUtil { private static PropertiesUtil propertiesUtil = null; private static final String SYNC_TIME = "syncTime"; private static final String SYNC_PATH = "syncPath"; private static final String LISTENER_PATH = "oldPath"; private static final long DEFAULT_TIME = 60000; private PropertiesUtil(){} public static PropertiesUtil getInstance() { if (propertiesUtil == null) propertiesUtil = new PropertiesUtil(); return propertiesUtil; } /** * * @Description : 加载配置文件 * @return Properties Created by [Aug 30, 2013] [1:52:41 PM] * @throws InterruptedException */ private Properties getSyncConfig() throws InterruptedException { Properties properties = new Properties(); try { properties.load(FileUtil.class .getResourceAsStream("/synchronous.properties")); } catch (IOException e) { logger.error("synchronous.properties文件不存在!"); throw new InterruptedException();//停止监听 } return properties; } /** * * @Description : 同步相隔时间 * @return long Created by [Aug 30, 2013] [1:52:56 PM] * @throws InterruptedException * @throws NumberFormatException */ public long getSyncTime() throws NumberFormatException, InterruptedException { return Long.valueOf(getSyncConfig().getProperty(SYNC_TIME, "" + DEFAULT_TIME)); } /** * * @Description : 文件同步路径 * @return * @throws Exception * String Created by [Aug 30, 2013] [2:07:51 PM] */ public String getSyncPath() throws InterruptedException { String path = getSyncConfig().getProperty(SYNC_PATH, ""); if (path.equals("")) throw new InterruptedException("请设置同步路径!");//停止监听 return path; } /** * * @Description : 文件监听路径 * @return * @throws Exception * String Created by [Aug 30, 2013] [2:07:38 PM] */ public String getListenerPath() throws InterruptedException { String path = getSyncConfig().getProperty(LISTENER_PATH); if (path.equals("")) throw new InterruptedException("请设置监听路径!");//停止监听 return path; } } 再次告诉大家一个秘密 java无法删除一个目录或者说非空目录 以前看很多人都去写一些复杂的代码 迭代文件夹然后一个一个去删除文件 这一样很麻烦 现在告诉大家一个高效的办法 可以直接使用runtime执行cmd命令中的 rd/s/q 文件目录 或文件夹 就可以删除此文件或文件夹
评论列表
本站所提供的代码,版权归原作者所有,若有侵犯作者版权,请与我们联系,我们将立即删除或修改。谢谢!
本站所有代码发布及提供者。
试试其它关键字
同语言下
.
Jquery搜索框获取回车事件
.
H5页面添加倒计时,然后自动跳转
.
通过user-agent判断h5页面是在哪个手机App(QQ、微信
.
nginx 禁止未绑定的域名访问
.
JavaScript 获取按键,并屏蔽系统 Window 事件
.
H5之只允许微信浏览器打开,禁止从外部浏览器访问
.
微信打开网址添加在浏览器中打开提示的办法
.
实现JS复制、粘贴,Chrome/Firefox下可用
.
video视频播放,play()、pause()、duration时长、onen
.
HTML5实现MP3上传前的预览和播放时长的获取
可能有用的
.
Jquery搜索框获取回车事件
.
H5页面添加倒计时,然后自动跳转
.
通过user-agent判断h5页面是在哪个手机App(QQ、微信
.
nginx 禁止未绑定的域名访问
.
JavaScript 获取按键,并屏蔽系统 Window 事件
.
H5之只允许微信浏览器打开,禁止从外部浏览器访问
.
微信打开网址添加在浏览器中打开提示的办法
.
实现JS复制、粘贴,Chrome/Firefox下可用
.
video视频播放,play()、pause()、duration时长、onen
.
HTML5实现MP3上传前的预览和播放时长的获取
邓诚
贡献的其它代码
(
1
)
.
实现服务器数据同步,并实时监听文件动态
地图
本站
我们
服务
版权
联系
回馈
博客