前述和需求说明

  和之前写的 Python:基于MD5的文件监听程序 是同样的功能,就不啰嗦了,就是又写了一个java版本的,可以移步 python 版本去看一下,整个的核心思路是一样的。代码已上传Github

类说明

  FileMd5.java 利用md5生成文件hash值
  fileWalk.java 只是一个文件遍历的demo,没有被其他类调用
  myFileListener.java 主程序,监控文件夹,用到了文件遍历,调用了FileMd5中的FileMd5类

代码

FileMd5.java

 package myFileListener;

 import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.FileInputStream;
import java.io.IOException; public class FileMd5 {
public static String fileMd5(String inputFile) throws IOException{ int bufferSize = 1024*1024; //缓冲区大小
FileInputStream fileInputStream = null;
DigestInputStream digestInputStream = null; try {
//获取MD5的实例
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); fileInputStream = new FileInputStream(inputFile); digestInputStream = new DigestInputStream(fileInputStream, messageDigest); //Creates a digest input stream, using the specified input stream and message digest. byte[] buffer = new byte[bufferSize]; //设置缓冲区,辅助读取文件,避免文件过大,导致的IO开销
while(digestInputStream.read(buffer)>0); //read: updates the message digest return int
// 获取最终的MessageDigest
messageDigest = digestInputStream.getMessageDigest();
// 拿到结果 return字节数组byte[] 包含16个元素
byte[] resultByteArray = messageDigest.digest(); return byteArrayToHex(resultByteArray); //转换byte 为 string 类型 }catch(NoSuchAlgorithmException e) {
return null;
}finally {
try {
digestInputStream.close();
fileInputStream.close();
}catch (Exception e) {
System.out.println(e);
}
}
} public static String byteArrayToHex(byte[] byteArray){
char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
//一个字节是八位二进制 也就是2位十六进制字符
char[] resultCharArray = new char[byteArray.length*2]; int index = 0;
for(byte b : byteArray){
resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b& 0xf];
}
return new String(resultCharArray);
}
}

myFileListener.java

 package myFileListener;

 import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import myFileListener.FileMd5; /**
* @author jyroo
* myfilelistener
*/
@SuppressWarnings("unused")
public class myFileListener {
HashMap<String, String> hashmap = new HashMap<>(); //存放hash键值对
List<String> file_in = new ArrayList<String>();
List<String> file_ex = new ArrayList<String>();
@SuppressWarnings("static-access")
public void iteratorPath(String dir, List<String> file_in, List<String> file_ex) {
while(true) {
List<String> pathName = new ArrayList<String>(); //存放文件名
File file = new File(dir);
File[] files = file.listFiles(); //返回某个目录下所有文件和目录的绝对路径 return file[]
if(files != null) {
for(File each_file : files) {
if(each_file.isFile()) { // 如果是文件
int jui=2, juj=2;
if(file_in.size()!=0) {
jui = 0;
for(String strin : file_in) {
if(each_file.getName().indexOf(strin)==-1) {
jui = 0;
}
if(each_file.getName().indexOf(strin)!=-1) {
jui = 1;
}
}
}
if(file_ex.size()!=0) {
juj = 0;
for(String strex : file_ex) {
if(each_file.getName().indexOf(strex)!=-1) {
juj = 1;
}
}
if(juj==1||jui==0) {
continue;
}
pathName.add(each_file.getName()); //存储文件名 String file_path = each_file.getAbsolutePath(); //获取文件的绝对路径 try {
FileMd5 mymd5 = new FileMd5();
String md5_value = mymd5.fileMd5(file_path); //生成文件对应的hash值
if(hashmap.get(each_file.getName())==null) {
System.out.println("文件夹:" + dir + "中的文件:" + each_file.getName() + "为新建文件!时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
hashmap.put(each_file.getName(), md5_value); //以文件名作为key,hash值作为value存储到hashmap中
}
if(!hashmap.get(each_file.getName()).equals(md5_value)) {
System.out.println("文件夹:" + dir + "中的文件:" + each_file.getName() + "被更新!时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
hashmap.put(each_file.getName(), md5_value);
}
} catch (Exception e) {
System.out.println("发生 "+e+" 的错误!!时间为" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
// }else if(each_file.isDirectory()) { //如果是文件夹
// //iteratorPath(each_file.getAbsolutePath()); //递归遍历
// }
}
}
try {
int juk;
for(String key : hashmap.keySet()) {
if(!pathName.contains(key)) {
System.out.println("文件夹:" + dir + "中的文件:" + key + "的文件已被删除!时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
hashmap.remove(key);
}
}
}catch(Exception e) {
System.out.println(e);
}
}
}
} public static void main(String[] args) {
myFileListener file_walk = new myFileListener();
List<String> file_ex = new ArrayList<String>();
List<String> file_in = new ArrayList<String>();
file_ex.add(".rec");
//file_in.add("hi");
file_walk.iteratorPath("E:\\tmp\\", file_in, file_ex);
for(String key:file_walk.hashmap.keySet()){
System.out.println("Key: "+key+" Value: "+file_walk.hashmap.get(key));
}
} }

fileWalk.java

package myFileListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test; @SuppressWarnings("unused")
public class fileWalk {
List<String> pathName = new ArrayList<String>();
public void iteratorPath(String dir) {
File file = new File(dir);
File[] files = file.listFiles(); //listFiles是获取该目录下所有文件和目录的绝对路径 return file[]
if(files != null) {
for(File each_file : files) {
if(each_file.isFile()) {
pathName.add(each_file.getName());
}else if(each_file.isDirectory()) {
iteratorPath(file.getAbsolutePath());
}
}
}
} public static void main(String[] args) {
fileWalk file_walk = new fileWalk();
file_walk.iteratorPath("E:\\tmp\\");
for(String list : file_walk.pathName) {
System.out.println(list);
}
}
}

Java:基于MD5的文件监听程序的更多相关文章

  1. Python:基于MD5的文件监听程序

    前述 写了一个基于MD5算法的文件监听程序,通过不同的文件能够生成不同的哈希函数,来实现实现判断文件夹中的文件的增加.修改.删除和过滤含有特定字符的文件名的文件. 需求说明 需要实现对一个文件夹下的文 ...

  2. 一个基于Socket的http请求监听程序实现

    首先来看以下我们的需求: 用java编写一个监听程序,监听指定的端口,通过浏览器如http://localhost:7777来访问时,可以把请求到的内容记录下来,记录可以存文件,sqlit,mysql ...

  3. 涂抹Oracle笔记1-创建数据库及配置监听程序

    一.安装ORACLE数据库软件及创建实例OLTP:online transaction processing 指那些短事务,高并发,读写频繁的数据库系统.--DB_BLOCK_SIZE通常设置较小.O ...

  4. 监听程序未启动或数据库服务未注册到该监听程序。启动该监听程序并注册数据库服务 然后重新运行 em configuration assistant。

    在WIN 7/64Bit上安装ORACLE 11gR2后,管理网页Database Control(如:https://localhost:1158/em)始终登录不进去,总是说密码错误,使用配置工具 ...

  5. (转)oracle 启动监听 报“监听程序不支持服务” 解决

    转自 http://www.51testing.com/html/99/478599-842622.html 今天安装了oracle后,启动监听,报错如下:    启动tnslsnr: 请稍候... ...

  6. (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...

  7. (转)解决 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    下面操作默认在安装Oralce数据库的服务器上运行. 1)确保Oracle 基本服务都已启动 OracleDBConsoleorcl OracleOraDb11g_home1TNSListener O ...

  8. ORA-12523: TNS: 监听程序无法找到适用于客户机连接的例程

    今天使用PL/SQL Developer连接到一台新的测试服务器时,遇到ORA错误:ORA-12523: TNS: 监听程序无法找到适用于客户机连接的例程.对应的监听日志文件里面错误为TNS-1252 ...

  9. Oracle几个基础配置问题:ORA-12154: TNS: 无法解析指定的连接标识符、ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务、ORA-12516 TNS监听程序找不到符合协议堆栈要求的可用处理程序

    问题1:ORA-12154: TNS: 无法解析指定的连接标识符 在一台服务器上部署了Oracle客户端,使用IP/SID的方式访问,老是报ORA-12154错误,而使用tnsnames访问却没有问题 ...

随机推荐

  1. Linux——浅析信号处理

    信号及其处理 信号处理是Unix和LInux系统为了响应某些状况而产生的事件,通常内核产生信号,进程收到信号后采取相应的动作. 例如当我们想强制结束一个程序的时候,我们通常会给它发送一个信号,然后该进 ...

  2. Java单例模式(Singleton)以及实现

    一. 什么是单例模式 因程序需要,有时我们只需要某个类同时保留一个对象,不希望有更多对象,此时,我们则应考虑单例模式的设计. 二. 单例模式的特点 1. 单例模式只能有一个实例. 2. 单例类必须创建 ...

  3. Python Cook函数笔记 【第一章】

    2017年4月28日 19:29:52 解压赋值给多个变量 可迭代的对象(list,tuple,string,文件对象,迭代器,生成器等),都可以进行解压赋值给多个对象. #!/usr/bin/env ...

  4. Java(五、类和对象中的例题)

    一.方法中的参数为数值型的(int) import java.util.Scanner; public class ScoreCalc { public void calc(int num1,int ...

  5. Springboot 框架学习

    Springboot 框架学习 前言 Spring Boot是Spring 官方的顶级项目之一,她的其他小伙伴还有Spring Cloud.Spring Framework.Spring Data等等 ...

  6. JaveScript基础(1)之变量和数据类型

    1.JaveScript变量的定义方式: A:隐式定义:直接给变量赋值: temp='hello'; alert(temp); PS:使用变量前要先进行初始化工作,否则会报变量未被定义的错误; B:显 ...

  7. bootgrid

    编写bootgrid前提条件  有关bootgrid的.css和.js库文件 参数:ajax:   必须设置为true  post:   传递给Java的参数  url:   与java连接的方法名  ...

  8. matplotlib简单的使用(二)

    1.折线图 import matplotlib as mlb from matplotlib import pylab as pl # 折线图 # 分别创建x,y坐标 x = [1,3,5,7,6,9 ...

  9. redis与python交互

    import redis #连接 r=redis.StrictRedis(host="localhost",port=6379,password="sunck" ...

  10. BloomFilter——大规模数据处理利器

    Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法.通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求100%正确的场合. 一.实例 为了说明Blo ...