前言
今天从cdlinux论坛中下载了一份密码字典,纯txt文本文件,一个密码一行,加起来有1.5亿行,2G+,但是我怀疑里面有重复的密码,所以想对文件内容进行去重处理。
分析
一般可能会想到一次将文本内容读取到内存中,用HashSet对内容去重,但是很不幸这个过程jvm会内存溢出,无奈,只能另想办法,首先将这个大文件中的内容读取出来,对每行String的hashCode取模取正整数,可用取模结果作为文件名,将相同模数的行写入同一个文件,再单独对每个小文件进行去重,最后再合并。
有内存溢出风险的写法:
public static void distinct() {
File ff = new File("G://password/all.txt");
File distinctedFile = new File("G://password/all-distinced.txt");
PrintWriter pw = null;
SetallHash = null;
FileReader fr = null;
BufferedReader br = null;
try {
pw = new PrintWriter(distinctedFile);
allHash = new HashSet();
fr = new FileReader(ff);
br = new BufferedReader(fr);
String l