和以前寫的 Python:基於MD5的文件監聽程序 是一樣的功能,就不囉嗦了,就是又寫了一個java版本的,能夠移步 python 版本去看一下,整個的核心思路是同樣的。代碼已上傳Githubhtml
FileMd5.java 利用md5生成文件hash值
fileWalk.java 只是一個文件遍歷的demo,沒有被其餘類調用
myFileListener.java 主程序,監控文件夾,用到了文件遍歷,調用了FileMd5中的FileMd5類java
1 package myFileListener; 2 3 import java.security.DigestInputStream; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 import java.io.FileInputStream; 7 import java.io.IOException; 8 9 public class FileMd5 { 10 public static String fileMd5(String inputFile) throws IOException{ 11 12 int bufferSize = 1024*1024; //緩衝區大小 13 FileInputStream fileInputStream = null; 14 DigestInputStream digestInputStream = null; 15 16 try { 17 //獲取MD5的實例 18 MessageDigest messageDigest = MessageDigest.getInstance("MD5"); 19 20 fileInputStream = new FileInputStream(inputFile); 21 22 digestInputStream = new DigestInputStream(fileInputStream, messageDigest); //Creates a digest input stream, using the specified input stream and message digest. 23 24 byte[] buffer = new byte[bufferSize]; //設置緩衝區,輔助讀取文件,避免文件過大,致使的IO開銷 25 while(digestInputStream.read(buffer)>0); //read: updates the message digest return int 26 // 獲取最終的MessageDigest 27 messageDigest = digestInputStream.getMessageDigest(); 28 // 拿到結果 return字節數組byte[] 包含16個元素 29 byte[] resultByteArray = messageDigest.digest(); 30 31 return byteArrayToHex(resultByteArray); //轉換byte 爲 string 類型 32 33 }catch(NoSuchAlgorithmException e) { 34 return null; 35 }finally { 36 try { 37 digestInputStream.close(); 38 fileInputStream.close(); 39 }catch (Exception e) { 40 System.out.println(e); 41 } 42 } 43 } 44 45 public static String byteArrayToHex(byte[] byteArray){ 46 char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; 47 //一個字節是八位二進制 也就是2位十六進制字符 48 char[] resultCharArray = new char[byteArray.length*2]; 49 50 int index = 0; 51 for(byte b : byteArray){ 52 resultCharArray[index++] = hexDigits[b>>> 4 & 0xf]; 53 resultCharArray[index++] = hexDigits[b& 0xf]; 54 } 55 return new String(resultCharArray); 56 } 57 }
1 package myFileListener; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.text.SimpleDateFormat; 6 import java.util.ArrayList; 7 import java.util.Date; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.List; 11 import java.util.Map; 12 import myFileListener.FileMd5; 13 14 /** 15 * @author jyroo 16 * myfilelistener 17 */ 18 @SuppressWarnings("unused") 19 public class myFileListener { 20 HashMap<String, String> hashmap = new HashMap<>(); //存放hash鍵值對 21 List<String> file_in = new ArrayList<String>(); 22 List<String> file_ex = new ArrayList<String>(); 23 @SuppressWarnings("static-access") 24 public void iteratorPath(String dir, List<String> file_in, List<String> file_ex) { 25 while(true) { 26 List<String> pathName = new ArrayList<String>(); //存放文件名 27 File file = new File(dir); 28 File[] files = file.listFiles(); //返回某個目錄下全部文件和目錄的絕對路徑 return file[] 29 if(files != null) { 30 for(File each_file : files) { 31 if(each_file.isFile()) { // 若是是文件 32 int jui=2, juj=2; 33 if(file_in.size()!=0) { 34 jui = 0; 35 for(String strin : file_in) { 36 if(each_file.getName().indexOf(strin)==-1) { 37 jui = 0; 38 } 39 if(each_file.getName().indexOf(strin)!=-1) { 40 jui = 1; 41 } 42 } 43 } 44 if(file_ex.size()!=0) { 45 juj = 0; 46 for(String strex : file_ex) { 47 if(each_file.getName().indexOf(strex)!=-1) { 48 juj = 1; 49 } 50 } 51 if(juj==1||jui==0) { 52 continue; 53 } 54 pathName.add(each_file.getName()); //存儲文件名 55 56 String file_path = each_file.getAbsolutePath(); //獲取文件的絕對路徑 57 58 try { 59 FileMd5 mymd5 = new FileMd5(); 60 String md5_value = mymd5.fileMd5(file_path); //生成文件對應的hash值 61 if(hashmap.get(each_file.getName())==null) { 62 System.out.println("文件夾:" + dir + "中的文件:" + each_file.getName() + "爲新建文件!時間爲:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 63 hashmap.put(each_file.getName(), md5_value); //以文件名做爲key,hash值做爲value存儲到hashmap中 64 } 65 if(!hashmap.get(each_file.getName()).equals(md5_value)) { 66 System.out.println("文件夾:" + dir + "中的文件:" + each_file.getName() + "被更新!時間爲:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 67 hashmap.put(each_file.getName(), md5_value); 68 } 69 } catch (Exception e) { 70 System.out.println("發生 "+e+" 的錯誤!!時間爲" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 71 } 72 } 73 // }else if(each_file.isDirectory()) { //若是是文件夾 74 // //iteratorPath(each_file.getAbsolutePath()); //遞歸遍歷 75 // } 76 } 77 } 78 try { 79 int juk; 80 for(String key : hashmap.keySet()) { 81 if(!pathName.contains(key)) { 82 System.out.println("文件夾:" + dir + "中的文件:" + key + "的文件已被刪除!時間爲:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 83 hashmap.remove(key); 84 } 85 } 86 }catch(Exception e) { 87 System.out.println(e); 88 } 89 } 90 } 91 } 92 93 public static void main(String[] args) { 94 myFileListener file_walk = new myFileListener(); 95 List<String> file_ex = new ArrayList<String>(); 96 List<String> file_in = new ArrayList<String>(); 97 file_ex.add(".rec"); 98 //file_in.add("hi"); 99 file_walk.iteratorPath("E:\\tmp\\", file_in, file_ex); 100 for(String key:file_walk.hashmap.keySet()){ 101 System.out.println("Key: "+key+" Value: "+file_walk.hashmap.get(key)); 102 } 103 } 104 105 }
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); } } }
原文出處:https://www.cnblogs.com/jyroy/p/10575190.htmlpython