Fastest way to load a file into a ArrayList?

I’m trying to load a 3.4MB file into a collection of some sort, where calling index 5 would return line 5 of the file. Also clean up such as removing whitespace and empty lines. My current code is a buffered reader that’s throwing everything into a single string, which then later gets split into the ArrayList. The first thing I tried (Scanner line by line) made it take like 10 minutes to load; Now it’s around 2-5.

What’s better for reading a file that’s going to be parsed? Scanner, BufferedReader, ByteReader?
What’s the best way to turn a large string into a collection?

BufferedReader reads lines. Read each line and put it in your list. It should complete 3.4MB in about 1 second.

if you want to read all lines of a file anyway, then there is a shortcut now with Java7.


  Path file = Paths.get("./blubb.txt");
  List<String> lines = Files.readAllLines(file, Charset.defaultCharset());

with this you lose all the boilerplate stream/reader and try/catch code