Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to determine length of a text file?

I have a data file which is comprised of rows of data, newline separated. I need to read the contents of the file into an array of Strings, and I would like to efficiently create the array at the correct size. Is it most efficient to

  1. Use an ArrayList,
  2. Scan through the file using BufferedReader, marking the start, counting lines and then reseting back to the mark, or
  3. ???
like image 741
TravisThomas Avatar asked Dec 13 '22 03:12

TravisThomas


1 Answers

Use an ArrayList (your option #1). Read in your text file line by line with BufferedReader's readLine() method. It's simple, efficient and maintainable.

like image 195
Asaph Avatar answered Jan 04 '23 23:01

Asaph