Search code examples
javastringparsingio

reading large files using apache common io


Hi I am using following code to get all the lines in a list of strings using the FileUtils.readLines functionality from Apache commons IO library. here is my code,

List<String> lines=FileUtils.readLines(new File(fileName));

But whenever i send a file say 45MB with 1 million lines it gives me an out of memory error. What should be the solution. I need to process each individual line.


Solution

  • Read one line after the other

    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
     try {
       while (it.hasNext()) {
        String line = it.nextLine();
         // do something with line
       }
     } finally {
      it.close();
     }