java推断文件是不是雷同的要领【JAVA教程】,java,文件
作者:搜教程发布时间:2019-12-13分类:JAVA教程浏览:36评论:0
导读:java推断文件是不是雷同的要领:1、盘算MD5或SHA-1然后对照推断//盘算文件的MD5值依据MD5值推断文件是不是是同一个文件publicst...

java推断文件是不是雷同的要领:
1、盘算MD5或SHA-1然后对照推断
// 盘算文件的 MD5 值 依据MD5值 推断文件是不是是同一个文件 public static String getFileMD5(File file) { if (!file.isFile()) { return null; } MessageDigest digest = null; FileInputStream in = null; byte buffer[] = new byte[8192]; int len; try { digest =MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer)) != -1) { digest.update(buffer, 0, len); } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } }
// 盘算文件的 SHA-1 值 依据SHA-1值 推断文件是不是是同一个文件 public static String getFileSha1(File file) { if (!file.isFile()) { return null; } MessageDigest digest = null; FileInputStream in = null; byte buffer[] = new byte[8192]; int len; try { digest =MessageDigest.getInstance("SHA-1"); in = new FileInputStream(file); while ((len = in.read(buffer)) != -1) { digest.update(buffer, 0, len); } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } }
2、直接推断内容是不是雷同
public class IOOperation { public static void main(String[] args) { FileInputStream File1 = null; FileInputStream File2 = null; BufferedReader in = null; String sFile; if(args.length != 2) { System.out.println("The command line should be: java IOOperation testX.txt testX.txt"); System.out.println("X should be one of the array: 1, 2, 3"); System.exit(0); } try { File1 = new FileInputStream(args[0]); File2 = new FileInputStream(args[1]); try { if(File1.available() != File2.available()) { //长度差别内容一定差别 System.out.println(args[0] + " is not equal to " + args[1]); } else { boolean tag = true; while( File1.read() != -1 && File2.read() != -1) { if(File1.read() != File2.read()) { tag = false; break; } } if(tag == true) System.out.println(args[0] + " equals to " + args[1]); else System.out.println(args[0] + " is not equal to " + args[1]); } } catch(IOException e) { System.out.println(e); } } catch (FileNotFoundException e) { System.out.println("File can't find.."); } finally { try { if(File1 != null) File1.close(); if(File2 != null) File2.close(); } catch (IOException e) { System.out.println(e); } } }
更多java学问请关注java基础教程栏目。
以上就是java推断文件是不是雷同的要领的细致内容,更多请关注ki4网别的相干文章!
相关推荐
- java经典面试题集锦(五)_JAVA教程,java,面试题
- java中的换行符是什么_JAVA教程,java,换行符
- Java中变量必须先定义后使用么_JAVA教程,java,变量
- java中怎么定义接口_JAVA教程,java,接口
- java中静态代码块有什么特点_JAVA教程,java,静态代码块
- java中return语句有什么作用_JAVA教程,java,return
- Java对文件的读写操作(图文详解)_JAVA教程,java
- java经典面试题集锦(四)_JAVA教程,java,面试题
- 八种基本数据类型分别是什么?_JAVA教程,java,基本数据类型
- java如何将字符串转为数组_JAVA教程,java,字符串,数组
你 发表评论:
欢迎- JAVA教程排行
-
- 1接口中只能定义常量和抽象方法,对么_JAVA教程,接口,常量,抽象方法
- 2java文件不能删除文件怎么办_JAVA教程,java
- 3系统提示javac:找不到文件_JAVA教程,系统,提示,javac,找不到,文件
- 4java中sleep的用法是什么?_JAVA教程,java,sleep
- 5java后台乱码怎么办_JAVA教程,java
- 6java图片显示不出来怎么办_JAVA教程,java
- 7java eclipse无法运行怎么办_JAVA教程,java
- 8javascript和java的区别是什么_JAVA教程,javascript,java
- 9Java中split()方法怎么用_JAVA教程,java,spilt,用法
- 最新文章
- 广而告之