how to read a file, line by line, without load all file on memory

534 views
Skip to first unread message

Augusto Vilarim

unread,
May 16, 2014, 10:40:35 AM5/16/14
to bea...@googlegroups.com
Hi Everyone!

First of all, sorry about my poor english. 

Well, i´m trying to read and parse a huge file with beanIO, and my concern is about memory allocation. When i do my brenchmark here, i notice that i dont need to load all beans on memory, i just need one single line per validation, there is some way to load one line per loop in memory, to avoid high memory usage? 

Thanks a lot!

Paul Rule

unread,
May 26, 2014, 7:01:12 AM5/26/14
to bea...@googlegroups.com
BeanIO doesn't need to read the whole file into memory - you just read one 'object'/line at a time.
If you look at the sample in the documentation:


BeanReader in = factory.createReader("employeeFile", new File("employee.csv"));
        Employee employee;
        while ((employee = (Employee) in.read()) != null) {
            // process the employee...
        }
        in.close();

The line
  employee = (Employee) in.read()
is just reading (streaming) one line at a time AFAIK.

If you read a massive file in this manner, hopefully you wont' see any significant memory usage. 
Reply all
Reply to author
Forward
0 new messages