📄 filemd5info.java
字号:
package com.md5.chen;
import java.io.*;
import java.security.*;
public class FileMD5Info
{
private static int BUFFER_SIZE = 32 *1024;
private static String[] hexDigits = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
private static String PATH;
public static void main(String []args)throws IOException
{
/*
String help = "Usage:java MD5Info";
help += "'s MD5Info files";
if(args.length == 0)
{
System.out.println(help);
return ;
}
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]+":"+md5(args[i]));
}
*/
//读取主目录的路径
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入文件夹路径:");
PATH = reader.readLine();// 将主目录路径保存再PATH中
try
{
// PATH=args[0];//以命令行参数为主路径
System.out.println("path=" + PATH);
System.out.println("MD5:"+md5(PATH));
} catch (Exception e)
{
e.printStackTrace();
System.out.println("路径名不合法!");
}
}
public static String md5(String f)
{
BufferedInputStream file;
MessageDigest md;
DigestInputStream in;
String digestString="";
try
{
// 打开文件
file = new BufferedInputStream(new FileInputStream(f) );
//创造一个MD5消息
md = MessageDigest.getInstance("MD5");
//Filter the file as a DigestInputStream object
in = new DigestInputStream(file,md);
int i;
byte[] buffer = new byte[BUFFER_SIZE];
//读取文件并处理
do
{
i = in.read(buffer,0,BUFFER_SIZE);
}
while(i == BUFFER_SIZE)
;
//Get the final digest and convert it to a String
md = in.getMessageDigest();
in.close();
byte[] digest = md.digest();
digestString = byteArrayToHexString(digest);
}
catch(FileNotFoundException e)
{
return f+" not found";
}
catch(NoSuchAlgorithmException e)
{
return "MD5 not supported.";
}
catch(IOException e)
{
return "Error reading from "+f+".";
}
return digestString;
}
//Convert a byte to a hexadecimal string
public static String byteToHexString(byte b)
{
int n= b;
if(n < 0)
{
n= 256 + n;
}
int d1 = n/16;
int d2 = n%16;
return hexDigits[d1]+hexDigits[d2];
}
//convert a byte array to a hexadecimal String
public static String byteArrayToHexString(byte[] b)
{
String result = "";
for (int i=0;i<b.length;i++)
{
result += byteToHexString(b[i]);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -