How to Read a file line by line using BufferedReader?

Hello Java developers.

I have studied java very well in last few months. A work is assigned to me in my company. Here I have many big text files. I have to read these files efficiently and the extract the business data and insert into database.

My problem is to find the best way to read the file in Java. I just searched the google and found that BufferedReader can be used.

So, help me and let's know How to Read a file line by line using BufferedReader, efficiently and using less memory.

Thanks

View Answers

February 9, 2013 at 12:12 AM

Hi,

You are right you can use the BufferedReader class for reading the big files which many range in many GB's is size.

It's not possible to read the whole file in memory. So, it's better to use the BufferedReader class.

The BufferedReader is present in java.io package. You can import

java.io.BufferedReader

in your program and use the class for reading the file line by line.

You can use the readLine() method of the class to read one line at a time very efficiently.

View the detailed example and code at Java Read File Line by Line - Java Tutorial page.

Thanks









Related Tutorials/Questions & Answers:
Advertisements