Home > database >  Java Out of memory heap space
Java Out of memory heap space

Time:09-29

Acutally i have a problem of java Out of memory heap space with my code below:

It works with medium sized csv files, on the other hand its plant with large csv files, with java out of memory error.

Thank you very much for your help :)

package routines;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.io.*;
import java.util.*;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
public class RemoveLineBreakFromCsvHelper {
public static void updateCSV (String fileToUpdate) throws IOException {
        File inputFile = new File(fileToUpdate);
        // Read existing file
        CSVReader reader = new CSVReader(new FileReader(inputFile), ';');
        List<String[]> csvBody = reader.readAll();
        System.out.println("Read CSV File");
       
        // get CSV row column and replace with by using row and column
        for(int i=0; i<csvBody.size(); i  ){
            String[] strArray = csvBody.get(i);
            for(int j=0; j<strArray.length; j  ){
           
                if(strArray[j].contains("\n")){ //String to be replaced
               
                System.out.println("Remove Line Breaks");
                    csvBody.get(i)[j] = strArray[j].replace("\n".toString(), ". "); //Target replacement
                   
                }
            }
        }
        reader.close();
        // Write to CSV file which is open
        CSVWriter writer = new CSVWriter(new FileWriter(inputFile), ';');
        writer.writeAll(csvBody);
        writer.flush();
        writer.close();
    }
}

CodePudding user response:

List<String[]> csvBody = reader.readAll();

If you're reading a large file, this reads a lot of data.

Instead, read each row one-by-one, and write them one-by-one:

String[] strArray;
while ((strArray = reader.readNext()) != null) {
  // ...

  writer.writeNext(strArray);
}

Since you're currently writing to the same file that you're reading from, you'd need to first write to a different file, and then move the new file to overwrite the old one.

  • Related