Currently I have:
- 1 file with 9 million lines
- BufferedReader.readLine() to read every line
- String.split() to parse every line (columns separated by a pipe)
- A lot of RAM used (because of String interning?)
The problem is: As you may have guessed, I want to read and parse this file a little better…
Questions:
- How do I read this relatively big file using the least amount of resources (knowing that every line will need some kind of “split” on pipe)?
- Can I replace String.split by something else (on lets say, StringBuilder, CharBuffer, …)?
- What’s the best way to avoid using Strings reading the file until I have split them to their final character sequence?
- I don’t mind using something else then String in my POJOs, if you have anything better?
- The file will be reload every few hours, if that helps you in giving me a solution?
Thank you 🙂
A 9 million line file should take less than a few seconds. Most of that time will be spent reading the data into memory. How you split up the data is unlikely to make much difference important.
The BufferedReader and String.split sounds fine to me. I wouldn’t use interning unless you are sure this will help. (It won’t intern() it for you)
The latest version of Java 6 has some performance improvements in the handling of Strings. I would try Java 6 update 25 to see if it is any faster.
EDIT: Doing some test finds that split is surprisingly slow and you cna improve on it.
prints
So it appears that splitting the string yourself is an improvement and gets you close to treading text as fast as you can. The only way to read it faster is to treat the file as binary/ASCII-7. 😉