I currently use:
BufferedReader input = new BufferedReader(new FileReader("filename"));
Is there a faster way?
I currently use:
BufferedReader input = new BufferedReader(new FileReader("filename"));
Is there a faster way?
Have you benchmarked your other options? I imagine that not using a BufferedReader may be faster in some cases - like extremely small files. I would recommend that you at the very least do some small benchmarks and find the fastest implementation that works with your typical use cases.
While what you've got isn't necessarily the absolute fastest, it's simple. In fact, I wouldn't use quite that form - I'd use something which allows me to specify a charset, e.g.
// Why is there no method to give this guaranteed charset
// without "risk" of exceptions? Grr.
Charset utf8 = Charset.forName("UTF-8");
BufferedReader input = new BufferedReader(
new InputStreamReader(
new InputStream("filename"),
utf8));
You can probably make it go faster using NIO, but I wouldn't until I'd seen an actual problem. If you see a problem, but you're doing other things with the data, make sure they're not the problem first: write a program to just read the text of the file. Don't forget to do whatever it takes on your box to clear file system caches between runs though...
Depends on what you want to read. The complete file, or from a specific location, do you need to able to seatch through it, or do you want to read the complete text in one go?
If it's /fast/ you want, keep the character data in encoded form (and I don't mean UTF-16). Although disc I/O is generally slow (unless it's cached), decoding and keeping twice the data can also be a problem. Although the fastest to load is probably through java.nio.channels.FileChannel.map(MapMode.READ_ONLY, ...)
, that has severe problems with deallocation.
Usual caveats apply.