Java ConcurrentHashMap多线程计算目录含子目录文件MD5

该博客展示了如何使用Java实现多线程计算目录下所有文件的MD5哈希值。通过创建`MD5File`类并使用`ConcurrentDirMD5`类扫描指定目录,程序能有效地并行处理每个文件,提高计算效率。同时,使用了`ConcurrentHashMap`确保线程安全。最后,输出所有文件及其对应的MD5值。
摘要由CSDN通过智能技术生成
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

class MD5File implements Runnable {
	private ConcurrentMap<String, String> map = null;
	private String filename = null;

	public MD5File(String file, ConcurrentMap<String, String> map) {
		this.filename = file;
		this.map = map;
	}

	@Override
	public void run() {
		File file = new File(filename);

		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			System.out.println("MessageDigest.getInstance");
			e.printStackTrace();
		}
		FileInputStream fs = null;
		try {
			fs = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("new FileInputStream");
			e.printStackTrace();
		}
		BufferedInputStream bs = new BufferedInputStream(fs);
		byte[] buffer = new byte[1024];
		int bytesRead;

		try {
			while ((bytesRead = bs.read(buffer, 0, buffer.length)) != -1) {
				md.update(buffer, 0, bytesRead);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("bs.read");
			e.printStackTrace();
		}
		byte[] digest = md.digest();

		StringBuilder sb = new StringBuilder();
		for (byte bite : digest) {
			sb.append(String.format("%02x", bite & 0xff));
		}
		map.put(filename, sb.toString());
	}

}

public class ConcurrentDirMD5 {
	private static final ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();

	public static ArrayList<String> fileArr = new ArrayList<String>();

	public static void scanDir(String path) throws Exception {
		File currentDir = new File(path);
		System.out.println(currentDir.getAbsolutePath());

		if (currentDir.isFile() && currentDir.canRead() && currentDir.length() <= 1024 * 1024 * 10) {

			fileArr.add(currentDir.getAbsolutePath());
			// System.out.println(currentDir.getAbsolutePath());
		} else {
			// System.out.println(currentDir.getAbsolutePath());
		}

		if (currentDir.isDirectory() && currentDir.canRead()) {
			String[] items = currentDir.list();
			if (items != null) {
				for (String name : items) {
					scanDir(path + "/" + name);
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		long starttime = System.currentTimeMillis();
		scanDir("c:\\windows\\appcompat\\Programs\\");
		ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
		for (String line : fileArr) {

			service.execute(new MD5File(line, map));
		}

		service.shutdown();
		while (!service.isTerminated()) {
			Thread.sleep(1);
		}

		long stoptime = System.currentTimeMillis();
		System.out.println(stoptime - starttime);

		List<String> values = new ArrayList<>(map.keySet());
		for (String val : values) {
			System.out.println(val + "," + map.get(val));
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值