| Up vote 6 down vote accepted |
The first point was that neither of those code samples read a file. This could sound fatuous or incorrect and it is true. What they actually does is the open a file for reading. And in terms of the what they actually does, there ' s probably not a huge difference in their respective efficiency.
When it comes to actually reading the file, the most approach to use would depend on what's the file contains, what form the Data have to is in for your in-memory algorithms, etc. This would determine whether it is better to use Scanner or a raw Reader , from a performance perspective and more Importan tly from the perspective of making your code reliable and maintainable.
Finally, the chances is, this won ' t make a significant difference to the overall performance of your code. What's I ' m saying is so you're optimizing your application prematurely. You is better of ignoring performance for now and choosing the version, that would make the rest of the your code simpler. When the application was working, profile it with some representative input data. The profiling is spent reading the file, in absolute terms, and relative to the rest of the Applica tion. This would tell you whether it was worth the effort to try to optimize the file reading.
The only bit of performance advice I ' d give was that character by character reading from an unbuffered input stream or read ER is inefficient. If the file needs to is, you should add a BufferedReader to the stack.
|