有内存溢出风险的写法:
public static void distinct() {
File ff = new File("G://password/all.txt");
File distinctedFile = new File("G://password/all-distinced.txt");
PrintWriter pw = null;
Set<String> allHash = null;
FileReader fr = null;
BufferedReader br = null;
try {
pw = new PrintWriter(distinctedFile);
allHash = new HashSet<String>();
fr = new FileReader(ff);
br = new BufferedReader(fr);
String line = null;
while((line=br.readLine())!=null){
line = line.trim();
if(line != ""){
allHash.add(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(null != fr){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null != br){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
for(String s:allHash){
pw.println(s);
}
pw.close();
}
jvm内存溢出:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.HashMap.newNode(HashMap.java:1734)
at java.util.HashMap.putVal(HashMap.java:630)
at java.util.HashMap.put(HashMap.java:611)
at java.util.HashSet.add(HashSet.java:219)
at encode.Main.distinct(Main.java:180)
at encode.Main.main(Main.java:215)
通过hashCode取模拆分写法:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
public class DistinctFileUtil {
public static File[] splitFile(String targetFile,int splitSize){
File file = new File(targetFile);
BufferedReader reader = null;
PrintWriter[] pws = new PrintWriter[splitSize];
File[] littleFiles = new File[splitSize];
String parentPath = file.getParent();
File tempFolder = new File(parentPath + File.separator + "test");
if(!tempFolder.exists()){
tempFolder.mkdir();
}
for(int i=0;i<splitSize;i++){
littleFiles[i] = new File(tempFolder.getAbsolutePath() + File.separator + i + ".txt");
if(littleFiles[i].exists()){
littleFiles[i].delete();
}
try {
pws[i] = new PrintWriter(littleFiles[i]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
tempString = tempString.trim();
if(tempString != ""){
//关键是将每行数据hash取模之后放到对应取模值的文件中,确保hash值相同的字符串都在同一个文件里面
int index = Math.abs(tempString.hashCode() % splitSize);
pws[index].println(tempString);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
for(int i=0;i<splitSize;i++){
if(pws[i] != null){
pws[i].close();
}
}
}
return littleFiles;
}
public static void distinct(File[] littleFiles,String distinctFilePath,int splitSize){
File distinctedFile = new File(distinctFilePath);
FileReader[] frs = new FileReader[splitSize];
BufferedReader[] brs = new BufferedReader[splitSize];
PrintWriter pw = null;
try {
if(distinctedFile.exists()){
distinctedFile.delete();
}
distinctedFile.createNewFile();
pw = new PrintWriter(distinctedFile);
Set<String> unicSet = new HashSet<String>();
for(int i=0;i<splitSize;i++){
if(littleFiles[i].exists()){
System.out.println("开始对小文件:" + littleFiles[i].getName() + "去重");
frs[i] = new FileReader(littleFiles[i]);
brs[i] = new BufferedReader(frs[i]);
String line = null;
while((line = brs[i].readLine())!=null){
if(line != ""){
unicSet.add(line);
}
}
for(String s:unicSet){
pw.println(s);
}
unicSet.clear();
System.gc();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1){
e1.printStackTrace();
} finally {
for(int i=0;i<splitSize;i++){
try {
if(null != brs[i]){
brs[i].close();
}
if(null != frs[i]){
frs[i].close();
}
} catch (IOException e) {
e.printStackTrace();
}
//合并完成之后删除临时小文件
if(littleFiles[i].exists()){
littleFiles[i].delete();
}
}
if(null != pw){
pw.close();
}
}
}
public static void main(String[] args) throws IOException {
int splitSize = 20;
File[] files = splitFile("G://test/bigfile.txt",splitSize);
distinct(files,"G://test/bigfile-distinct.txt",splitSize);
}
}
总结
本篇文章的内容就到这了,希望大家可以喜欢,也希望大家可以多多关注编程网的其他精彩内容!