java的,我已经测试可以使用。
但是单个文件很大的话可能算md5会比较慢
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Main
{
ArrayList<String> md5List = new ArrayList<String>();
public String getMd5(File file){
String value = null;
FileInputStream in = null;
try {
in = new FileInputStream(file);
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
public void run(File file) {
if(file != null){
if(file.isDirectory()){
File f[] = file.listFiles();
if(f != null) {
for(int i = 0; i < f.length; i++)
run(f[i]);
}
} else {
String md5 = getMd5(file);
if(md5List.contains(md5)) {
System.out.println(file.toString());
file.delete();
} else {
md5List.add(md5);
}
}
}
}
public static void main(String args[]) {
Main m = new Main();
String path = "D:\\test";
m.run(new File(path));
}
}