巨大なテキスト ファイルを読み書きしようとしています。しかし、私がそれをしようとすると、エラーが発生します:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at ReadWriteTextFile.getContents(ReadWriteTextFile.java:52)
at ReadWriteTextFile.main(ReadWriteTextFile.java:148)
私のコードは次のとおりです。
import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class ReadWriteTextFile {
/**
* Fetch the entire contents of a text file, and return it in a String.
* This style of implementation does not throw Exceptions to the caller.
*
* @param aFile is a file which already exists and can be read.
*/
static public String getContents(File aFile) {
//...checks on aFile are elided
StringBuilder contents = new StringBuilder();
int maxlines = 1000; //counts max lines t read/write to the file
BufferedReader input = null;
BufferedWriter bw = null;
try {
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
input = new BufferedReader(new FileReader(aFile));
try {
String line = null; //not declared within while loop
/*
* readLine is a bit quirky :
* it returns the content of a line MINUS the newline.
* it returns null only for the END of the stream.
* it returns an empty String if two newlines appear in a row.
*/
//for (int i = 0; i < 100; i++){
//int count = 0;//initiates the line counter
while (( line = input.readLine()) != null){
int count = 0;//initiates the line counter
String modified1 = line.substring(2,17);
String modified2 = line.substring(18,33);
String modified3 = line.substring(40);
String result = "empty";
result = modified1 + ",," +modified2 + modified3;
System.out.println (result);
// contents.append(line);
// contents.append(System.getProperty("line.separator"));
//int count = 0;//initiates the line counter
try {
contents.append(line);
contents.append(System.getProperty("line.separator"));
String content = result;
File file = new File("C:\\temp\\out.txt");//output path
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
for ( int i = 0; i < 1000; i++){
if (count++ % maxlines == 0) {
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
}
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//}
}
}
finally {
input.close();
bw.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
/**
* Change the contents of text file in its entirety, overwriting any
* existing text.
*
* This style of implementation throws all exceptions to the caller.
*
* @param aFile is an existing file which can be written to.
* @throws IllegalArgumentException if param does not comply.
* @throws FileNotFoundException if the file does not exist.
* @throws IOException if problem encountered during write.
*/
static public void setContents(File aFile, String aContents)
throws FileNotFoundException, IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: " + aFile);
}
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: " + aFile);
}
//use buffering
Writer output = new BufferedWriter(new FileWriter(aFile, true));
try {
//FileWriter always assumes default encoding is OK!
output.write( aContents );
}
finally {
output.close();
}
}
/** Simple test harness. */
public static void main (String... aArguments) throws IOException {
File testFile = new File("C:\\temp\\in.txt");//input path
System.out.println("\n" + getContents(testFile));
}
}
一定量の行が読み取られた後にバッファーをフラッシュできるように、カウンター (カウント) を追加しようとしました。うまくいきませんでした。カウンターが正しく機能しないことはわかっています。ゼロにならない 「while」ループの特別な回数の実行後。while ループの前後に「for」ループを追加してカウンターを空にしましたが、うまくいきませんでした。
なにか提案を?