1

I have a comma delimited CSV file ans I have the code to read it from 2nd line. But it gives me the following error. Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

This is my Code.

public static List<String> readCSV(String path){        
    BufferedReader br = null;
    String[] cd = null;
    String line ="";
    String split=",";
    List<String> list = new ArrayList<String>();
    try {        
        br=new BufferedReader(new FileReader(path));            
        while ((line = br.readLine()) != null) {
            boolean firstLine = true;               
            while (line != null && line.startsWith("child's Last Name")) {
                if (firstLine) {
                    firstLine = false;
                    continue;
                } else {
                    list.add(line);
                }
            }               
        }               
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return list;
}   

How can I fix it with this code..

Thanks in Advance!

user2999888
  • 65
  • 1
  • 2
  • 8

5 Answers5

1

In your second while loop where are you assigning line again

 while (line != null && line.startsWith("child's Last Name"))

Once this condition is satisfied, you are not reassigning it.
Your code should be different something like,

boolean firstLine = true;               
while ((line = br.readLine()) != null) 
{
   if (firstLine) 
   {
      firstLine = false;
       continue;
   } 
   else if(line != null && line.startsWith("child's Last Name"))
   {
        list.add(line);
   }
}    
dbw
  • 5,842
  • 2
  • 21
  • 56
0

Usually, you get an out of memory error when you... well... run out of memory to run the application. Have you debugged into it? You could be stuck in an endless loop...

while (line != null && line.startsWith("child's Last Name")) {

Should this be an if statement?

EDIT:

while ((line = br.readLine()) != null) {

This really should be:

while(br.hasNext()) {

More info: Java - Read CSV with Scanner()

Community
  • 1
  • 1
Ben Dale
  • 2,172
  • 12
  • 14
0

Your problem has nothing to do with the 2nd line; You're simply trying to put the whole file in list and eventually, there's just too much to fit in memory. You have two choices:

  • Do not keep it all in memory! Process the line, do what you must, and don't store it, so it eventually gets garbage collected
  • If you really really must keep it all in memory, try increasing the amount of heap space available by starting java with a -Xmx parameter (run java -X for details)

UPDATE: oh oops, yes, as others said, you're also stuck in and endless loop. Consider this answer only once you've fixed that!

Miquel
  • 14,565
  • 7
  • 49
  • 83
0

try

    br=new BufferedReader(new FileReader(path));            
    boolean firstLine = true;               
    while ((line = br.readLine()) != null) {
        if (line.startsWith("child's Last Name")) {
            if (firstLine) {
                firstLine = false;
                continue;
            } 
            list.add(line);
        }               
    }   

There is no need for two while loops

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
-1

try this..

    br=new BufferedReader(new FileReader(path)); 
    br.readLine();           
    while ((line = br.readLine()) != null) {
         if (line.startsWith("child's Last Name")) {
                 list.add(line);
         }               
    }      
subash
  • 3,061
  • 2
  • 14
  • 20