JNotify文件夹监管
来源:互联网 发布:淘宝直播开通后怎么办 编辑:程序博客网 时间:2024/05/06 06:04
JNotify对文件夹进行监管添加文件、删除文件、文件重命
下载好JNotify,里面有两个文件分别是jnotify.dll和jnotify_64bit.dll ,将这两个文件放到C:\Program Files\Java\jre7\bin下面就好(本地安装的jre的bin目录下,我用的是ssm框架)
public class TestFile {
public static void main(String[] args) {
//监控文件夹是否发生变化 下满这个方法不合适
try {
System.out.println(“进入监管”);
new TestFile().sampleTest();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sampleTest() throws Exception{ //监控文件路径 String path = "D:\\test"; //设置参数 指定你关心的事件 或者 JNotify.FILE_ANY 任意事件 int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED ; //好像是观察子目录 boolean watchSubtree = true; //最后一个参数 继承了JNotifyListener 代码在最后 int watchId = JNotify.addWatch(path, mask, watchSubtree, new Listener() ); //返回一个watch的ID //Thread.sleep(5000);//监管时间设置 //boolean resule = JNotify.removeWatch(watchId);//通过watchID删除watch //if(!resule){ //删除失败 } //sampleTest();//如果不回调一下,监管时间结束后就不会在重新进入监管模式}//拷贝方法 新增文件后 拷贝到另一目录下 操作如下public void saveServer(String oldpath ){ try { String newPath="D:\\newTest";//保存到新的文件夹路径 File newFile = new File(newPath);//得到新文件夹对象 if(!newFile.exists()){ (new File(newPath)).mkdirs(); //没有文件夹 就创建一个 } File oldFile = new File(oldpath); String[] oldFileList = oldFile.list();//得到旧的文件夹中的文件路径 File temp = null; for(int i = 0; i<oldFileList.length;i++){//循环的得到旧文件夹中的所有文件并复制到新文件夹中 if(oldpath.endsWith(File.separator)){//判断路径最后是不是以“\”结尾 temp = new File(oldpath+oldFileList[i]); }else{ temp = new File(oldpath+File.separator+oldFileList[i]); } if(temp.isFile()){ String name =newPath+"\\"+(temp.getName().toString()); System.out.println("加上文件名后的新路径"+name); File fileN = new File(name); if(!fileN.exists()){ FileOutputStream out = new FileOutputStream(name); FileInputStream input = new FileInputStream(temp); byte[] by = new byte[1024]; int len; try { while ((len=input.read(by)) != -1) { out.write(by, 0, len); } out.flush(); out.close(); input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}
//继承了JNotifyListener
public class Listener implements JNotifyListener {
@Override
public void fileRenamed(int arg0, String arg1, String arg2, String arg3) {
// TODO Auto-generated method stub
System.out.println(“renamed”+arg0+arg1+”——”+arg2+”——–”+arg3);
}
@Overridepublic void fileModified(int arg0, String arg1, String arg2) { // TODO Auto-generated method stub System.out.println("modified"+arg0+arg1+"------"+arg2);}@Overridepublic void fileDeleted(int arg0, String arg1, String arg2) { // TODO Auto-generated method stub System.out.println("deleted"+arg0+arg1+"------"+arg2);}@Overridepublic void fileCreated(int arg0, String oldpath, String arg2) { System.out.println("created文件夹新增文件"+arg0+oldpath+"------"+arg2); TestFile tt = new TestFile(); tt.saveServer(oldpath); //调用 拷贝方法 }
}
- JNotify文件夹监管
- JNotify
- JNotify使用
- JNotify使用
- JNotify使用
- JNotify的使用
- JAVA缓存技术-jnotify
- JNotify的使用
- 使用JNotify监控硬盘
- JNotify的使用
- JNotify监听文件变化
- JNotify使用 样例
- JNotify文件目录监控
- JNotify文件变化通知
- JNotify INotify windows&Linux
- jNotify:操作结果信息提示条
- JNotify使用实例与BUG修复
- JNotify和EHcache实现缓存技术
- spring aop实现--注解实现和配置实现
- excel入门,如何玩转excel,你早该这么玩Excel笔记6
- linux C++ make 实战--建立一个最简单的工程
- C#练习——统计整型数组中不重复数字的个数、将普通日期格式转换成汉字日期格式、在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数
- HDU 1094
- JNotify文件夹监管
- if语句后面的分号
- 2724: [Violet 6]蒲公英
- 动态规划、记忆化搜索:HDU1505-City Game(记忆化搜索)
- MyBatis入门基础(一)
- springboot (1) 快速搭建简单项目
- arcengine栅格渲染(栅格重分类) IRasterClassifyColorRampRenderer
- php 去掉最大值,最小值,求出平均值(简洁代码)
- Uboot 2017.01 SPL中的image_loader