java - Scanner vs InputStreamReader - Stack Overflow

Scanner vs InputStreamReader

Ask Question

up vote
10
down vote

favorite

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"));

java file input

 

1  

Related: stackoverflow.com/questions/2231369/scanner-vs-bufferedreade??r – Paul Bellora Apr 8 ‘12 at 23:46
    

Related: Scanner v. StreamTokenizer. – trashgod Apr 9 ‘12 at 0:09

add a comment | 

3 Answers 3

active oldest votes


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.

 

   

add a comment | 


up vote 3 down vote

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.

 

   

add a comment | 


up vote 2 down vote

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

add a comment | 


asked


5 years, 1 month ago


viewed


10752 times


active


3 years, 6 months ago

Blog


来自为知笔记(Wiz)

时间: 2024-11-06 19:22:15

java - Scanner vs InputStreamReader - Stack Overflow的相关文章

Stack Overflow上59万浏览量的提问:为什么会发生ArrayIndexOutOfBoundsException?

在逛 Stack Overflow 的时候,发现了一些访问量像昆仑山一样高的问题,比如说这个:为什么会发生 ArrayIndexOutOfBoundsException?这样看似简单到不值得一问的问题,访问量足足有 69万+,这不得了啊!说明有不少的初级程序员被这个问题困扰过.实话实说吧,我也有点吃不准为什么. 来回顾一下提问者的问题: ArrayIndexOutOfBoundsException 究竟意味着什么?我该如何摆脱这个错误. 如果你也曾被这个问题困扰过,或者正在被困扰,就请随我一起来

(转)Stack Overflow 2016最新架构探秘

这篇文章主要揭秘 Stack Overflow 截止到 2016 年的技术架构. 首先给出一个直观的数据,让大家有个初步的印象. 相比于 2013 年 11 月,Stack Overflow 在 2016 年 02 月统计数据有较大变化,下面给出 2016 年 02 月 09 号一天的数据,如下: HTTP 请求数 209,420,973 (+61,336,090) 网页加载次数 66,294,789 (+30,199,477) HTTP 流量发送有1,240,266,346,053 (+406

Stack Overflow 2017 开发者调查报告

Stack Overflow 发布了 2017 开发者调查报告,此次有超过 64,000 名开发人员参与调查,分别对其技能.工具.学习趋势等数据进行了统计,现将其中一些有趣的数据和趋势撷取出来分享给大家. 一.开发角色 开发类型 大约有四分之三的受访者是 web 开发人员,不过这其中也有许多人表示正在努力构建桌面应用和移动应用. 具体开发类型 二.开发经验 Web 和移动开发人员平均而言,比其他技术学科的开发人员(如系统管理和嵌入式编程)的专业编码经验要少得多.软件行业是新人才的主要孵化器,经验

Stack Overflow 上排名前十的与API相关的问题

Stack Overflow是一个庞大的编程知识仓库,在Stack Overflow 上,数百万的提问被回答,并且这些回答都是高质量的.这就是为什么在Google搜索结果的排行榜上,Stack Overflow 总是位居首位. 虽然Stack Overflow上有非常多的提问,但是仍然每天都有大量的问题被提出,其中的很多都等待解答或者没有得到好的解答.因此,问题是如何找到答案的,通过Stack Overflow是不够的. 随着成千上万的开发者使用Java的API并且在Github上分享他们的项目

Stack Overflow 2016最新架构探秘

这篇文章主要揭秘 Stack Overflow 截止到 2016 年的技术架构. 首先给出一个直观的数据,让大家有个初步的印象. 相比于 2013 年 11 月,Stack Overflow 在 2016 年 02 月统计数据有较大变化,下面给出 2016 年 02 月 09 号一天的数据,如下: HTTP 请求数 209,420,973 (+61,336,090) 网页加载次数 66,294,789 (+30,199,477) HTTP 流量发送有1,240,266,346,053 (+406

Stack的三种含义(数据超过栈的大小,就发生stack overflow)

非常典型的基础知识,转自http://www.ruanyifeng.com/blog/2013/11/stack.html 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词其实有三种含义,适用于不同的场合,必须加以区分. 含义一:数据结构 stack的第一种含义是一组数据的存放方式,特点为LIFO,即后进先出(Last in, first out). 在这种数据结构中,数据像积木那样一层层堆起来,后面加

Stack Overflow: The Architecture - 2016 Edition

To get an idea of what all of this stuff “does,” let me start off with an update on the average day at Stack Overflow. So you can compare to theprevious numbers from November 2013, here’s a day of statistics from February 9th, 2016 with differences s

国内为什么没有好的 Stack Overflow 的模仿者?

国内为什么没有好的 Stack Overflow 的模仿者? 个人觉得, 高端的程序员会直接上stackoverflow提问, 所以国内中文的stackoverflow必然面对低端程序员. 鉴于中国程序员平均水平, 个人觉得面向低端程序员的中文stackoverflow有存在的价值. 搭建站点的技术难度其实不高, 能够完成这个任务的社区也很多, 但是为什么现在没有特别出众的出现? 3 条评论 分享 按投票排序按时间排序 43 个回答 赞同26反对,不会显示你的姓名 王雷,程序员.创业者 韩伟.M

log4j-over-slf4j与slf4j-log4j12共存stack overflow异常分析

注:下文中的"桥接"."转调"."绑定"等词基本都是同一个概念. log4j-over-slf4j和slf4j-log4j12是跟java日志系统相关的两个jar包,当它们同时出现在classpath下时,就可能会引起堆栈溢出异常.异常信息大致如下(摘自slf4j官网文档Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting St