형식이 올바른 출력을 새로운 텍스트 파일에 쓰려면 올바르게 포맷하려면 큰 텍스트 파일 (약 600MB)을 처리해야합니다. 문제는 새로운 파일에 내용을 쓰는 것이 약 6.2 MB에서 멈추는 것입니다. 여기에 코드입니다 :Java - 텍스트 파일 작성을 완료 할 수 없습니다.
/* Analysis of the text in fileName to see if the lines are in the correct format
* (Theme\tDate\tTitle\tDescription). If there are lines that are in the incorrect format,
* the method corrects them.
*/
public static void cleanTextFile(String fileName, String destFile) throws IOException {
OutputStreamWriter writer = null;
BufferedReader reader = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(destFile), "UTF8");
} catch (IOException e) {
System.out.println("Could not open or create the file " + destFile);
}
try {
reader = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
System.out.println("The file " + fileName + " doesn't exist in the folder.");
}
String line;
String[] splitLine;
StringBuilder stringBuilder = new StringBuilder("");
while ((line = reader.readLine()) != null) {
splitLine = line.split("\t");
stringBuilder.append(line);
/* If the String array resulting of the split operation doesn't have size 4,
* then it means that there are elements of the news item missing in the line
*/
while (splitLine.length != 4) {
line = reader.readLine();
stringBuilder.append(line);
splitLine = stringBuilder.toString().split("\t");
}
stringBuilder.append("\n");
writer.write(stringBuilder.toString());
stringBuilder = new StringBuilder("");
writer.flush();
}
writer.close();
reader.close();
}
이미 답을 검토 한 결과,하지만 문제는 일반적으로 작가가 폐쇄되지 않는 사실 또는 flush()
방법의 부재와 관련이 있습니다. 따라서 BufferedReader에 문제가 있다고 생각합니다. 내가 뭘 놓치고 있니? 이 루프에서
..? – OmniOwl
나는 처음에는 특정 횟수만큼 (정확히는 500 회) 플러시를 시도했다.주기의 모든 반복에서 플러시를 피하기를 희망했지만 작동하지 않았다. 플러시를 사용하는 올바른 방법은 무엇입니까? – Judas
입력 파일 (600MB의 레코드)에서 최소한 일부 레코드를 제공 할 수 있습니까? – Jagger