Scanner vs InputStreamReader Ask Question
Up vote vote favorite |
Does anyone happen to know if there are any difference with regards to performance between the both methods of reading input File below? Thanks. 1) Reading a file with Scanner and file Scanner input =newScanner(newFile("foo.txt"));
2) Reading a file with InputStreamReader and FileInputStream InputStreamReader input =newInputStreamReader(newFileInputStream("foo.txt"));
Java file input |
|
related:stackoverflow.co M/questions/2231369/scanner-vs-bufferedreade?? R – paul bellora apr 8 ' at 23:46 |
|
|
Related:scanner v. Streamtokenizer. –trashgod Apr 9 ' at 0:09 |
Add a Comment | |
3 Answers3Active oldest votes
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. |
|
Add a comment | |
up vote 3 down vote |
in terms of performance, Scanner was definitely the slower one , at least from my experience. It ' s made for parsing, not reading huge blocks of data. InputStreamReader, with a large enough buffer, can perform in par with BufferedReader, which I remember-be a few times Faster than Scanner for reading from a dictionary list. Here's a comparison between BufferedReader and InputStreamReader. Remember that BufferedReader is a few times faster than Scanner. |
|
Add a comment | |
Up vote 2 down vote |
A difference, and the principal, I guess, is so with the bufferedreader/inputstreamreader you can read the whole documen t character by character, if you want. With scanner this is no possible. It means that with the InputStreamReader can has more control about the content of the document. ;) |
|
this answer doesn ' t addre SS performance, though, which is what the OP was asking. – adam mihalcin apr 9 ' at 0:14 |
|
|
@AdamMihalcin If your point is, the answer If Off-topic I disagree. He wants to hear about performance and also gets the know the difference between the both which would ultimately only Benefi TS His understanding of the difference between the. –joop Apr ' at 10:18 |
Add a Comment | |
Asked |
5 years, 1 month ago |
Viewed |
10752 Times |
Active |
3 years, 6 months ago |
Blog
From for notes (Wiz)
Java-scanner vs Inputstreamreader-stack Overflow