java - Scanner vs InputStreamReader - Stack Overflow

本文探讨了Java中使用Scanner与InputStreamReader读取文件的性能差异。指出Scanner更适合解析小规模数据,而InputStreamReader结合BufferedReader能更高效地处理大量数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 
 

Does anyone happen to know if there is any difference with regards to performance between the two 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"));
 
up vote 6 down vote accepted

The first point is that neither of those code samples read a file. This may sound fatuous or incorrect, but it is true. What they actually do is open a file for reading. And in terms of what they actually do, there's probably not a huge difference in their respective efficiency.

When it comes to actually reading the file, the best approach to use will depend on what the file contains, what form the data has to be in for your in-memory algorithms, etc. This will determine whether it is better to use Scanner or a raw Reader, from a performance perspective and more importantly from the perspective of making your code reliable and maintainable.

Finally, the chances are that this won't make a significant difference to the overall performance of your code. What I'm saying is that you are optimizing your application prematurely. You are better of ignoring performance for now and choosing the version that will make the rest of your code simpler. When the application is working, profile it with some representative input data. The profiling will tell you the time is spent reading the file, in absolute terms, and relative to the rest of the application. This will tell you whether it is worth the effort to try to optimize the file reading.

The only bit of performance advice I'd give is that character by character reading from an unbuffered input stream or reader is inefficient. If the file needs to be read that way, you should add a BufferedReader to the stack.

 
  
 

In terms of performance, Scanner is 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 on par with BufferedReader, which I remember to 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.

 
  

A difference, and the principal, I guess, is that with the BufferedReader/InputStreamReader you can read the whole document character by character, if you want. With scanner this is no possible. It means that with the InputStreamReader you can have more control about the content of the document. ;)

 
2 
This answer doesn't address performance, though, which is what the OP was asking. –  Adam Mihalcin Apr 9 '12 at 0:14
   
@AdamMihalcin If your point is that the answer if off-topic I disagree. He wants to hear about performance and also gets the know the difference between the two which will ultimately only benefits his understanding of the difference between the two.. –  Joop Apr 25 '15 at 10:18

 





转载于:https://www.cnblogs.com/gomygo/p/6844519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值