초보 자 에 게 자바 를 사용 하여 큰 텍스트 파일 의 내용 을 어떻게 재 구성 하 는 지 가르쳐 줍 니 다.
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 {
/**
* hash
* @param targetFile
* @param splitSize hash
* @return
*/
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;
}
/**
*
* @param littleFiles
* @param distinctFilePath
* @param splitSize
*/
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);
}
}
총결산이 글 의 내용 은 여기까지 입 니 다.여러분 들 이 좋아 하 시 기 를 바 랍 니 다.여러분 들 도 우리 의 다른 멋 진 내용 에 많은 관심 을 가 져 주 셨 으 면 좋 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.