How do I read a large file quickly in Java?

Hi,

I my project I have to read vendor feed which comes in text file. Size of the file is usually in 1-2 GB in size. What is the best way to read the file efficiently?

How do I read a large file quickly in Java?

Thanks

View Answers

April 5, 2017 at 1:24 PM

Hi,

You can't read the whole file in memory and process it line by line. The memory size is a constraint for the computer and even for servers. So, only one method is possible and it is of reading the file line by line.

In Java you can use the BufferedReader class for reading the file line by line. Reading file one line at a time using the BufferedReader class uses very less memory and the program works very fast.

Check the tutorial: Java Read File Line by Line.

The Scanner class can also be used for reading the file line by line. Check this tutorial: Line by Line reading from a file using Scanner Class.

Thanks









Related Tutorials/Questions & Answers:
Advertisements