简体   繁体   English

有没有理由不将InputStreamReader与BufferedReader包装在一起?

[英]Is there ever a reason not to wrap InputStreamReader with BufferedReader?

I have the following code to read from a CSV: 我有以下代码要从CSV中读取:

InputStream inp = getClass().getResourceAsStream(filename);
InputStreamReader r = new InputStreamReader(inp);
BufferedReader reader = new BufferedReader(r);

On answered questions: Java BufferedReader , Convert InputStream to BufferedReader , What is the difference between Java's BufferedReader and InputStreamReader classes? 关于已回答的问题: Java BufferedReader将InputStream转换为BufferedReaderJava的BufferedReader和InputStreamReader类之间有什么区别?

BufferedReader[BR] and InputStreamReader[ISR] both implement the same interfaces. BufferedReader [BR]和InputStreamReader [ISR]都实现相同的接口。 BR has every method that ISR has with the additional methods including the ever so useful readLine() method and less useful but still relevant skip() method. BR具有ISR拥有的所有方法以及其他方法,包括曾经如此有用的readLine()方法和不太有用但仍具有相关性的skip()方法。 You don't necessarily need BR to read single characters although BR can do the same more efficiently than ISR in this respect. 尽管在这方面,BR可以比ISR更为有效,但您不一定需要BR读取单个字符。 The only significant difference is that FileReader is a subclass of ISR but not BR, although I have had sources on this website say that FileReader isn't really used anymore due to alternatives. 唯一的显着区别是FileReader是ISR的子类,但不是BR的子类,尽管我在该网站上有消息说,由于替代方案,FileReader不再被真正使用。

My research says that everything ISR can do is done better by BR. 我的研究表明,BR可以更好地完成ISR的所有工作。 I am a young developer so every defined or imported class to me seems relevant. 我是一个年轻的开发人员,所以对我来说每个定义或导入的类都似乎很重要。 What I am trying to grasp is if some classes are no longer used, with new versions or frameworks replacing them. 我要掌握的是是否不再使用某些类,而用新版本或框架替换它们。 I want to know what more experienced developers have to say. 我想知道更有经验的开发人员该怎么说。 SO, is there a reason to not use BR when using ISR? 因此,使用ISR时是否有理由不使用BR?

QuickLinks to API: API的快速链接:
BufferedReader 的BufferedReader
InputStreamReader InputStreamReader中


I see some confusion in your post about ISR and BR. 我在您的帖子中对ISR和BR感到有些困惑。

1) You are saying that 1)你是说

My research says that everything ISR can do is done better by BR 我的研究表明,BR可以更好地完成ISR的所有工作

But lets look at JavaDoc for each of them: 但是让我们来看一下其中的每个JavaDoc:

ISR ISR

public class InputStreamReader extends Reader 公共类InputStreamReader扩展Reader

An InputStreamReader is a bridge from byte streams to character streams: InputStreamReader是从字节流到字符流的桥梁:

BR BR

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. 从字符输入流中读取文本,缓冲字符,以便有效读取字符,数组和行。

As you can see ISR converts bytes to chars. 如您所见, ISR将字节转换为字符。 BR on other hand needs chars. 另一方面, BR需要使用字符。 Thats why BR needs to use ISR to read from InputStream . 这就是为什么无线电通信局需要使用ISR读取InputStream

2) As to the original question why not to just use ISR. 2)关于最初的问题,为什么不仅仅使用ISR。 You can definately do that, but in order to gain performance you want to use BR. 您绝对可以这样做,但是为了获得性能,您想使用BR。 You might ask why ISR was not implemented using buffering? 您可能会问为什么未使用缓冲实现ISR? Because ISR is designed to do one thing good and that is to read and convert bytes to chars. 因为ISR被设计为做一件好事,那就是读取字节并将其转换为char。 The buffering part is moved into the Decorator class that is BR. 缓冲部分被移到BR的Decorator类中。 This is done in order to be able to add buffering capabilities to any Reader and not only ISR. 这样做是为了能够将缓冲功能添加到任何Reader,而不仅是ISR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取所有字符时,是否有理由在InputStreamReader上使用BufferedReader? - Is there a reason to use BufferedReader over InputStreamReader when reading all characters? 缓冲读取器或输入流读取器中的gc开销 - gc overhead in bufferedreader or inputstreamreader 关闭BufferedReader和InputStreamReader - Closing BufferedReader and InputStreamReader Java中的BufferedReader和InputStreamReader - BufferedReader and InputStreamReader in Java Mocking InputStream、InputStreamReader 和 BufferedReader - Mocking InputStream, InputStreamReader and BufferedReader java InputStreamReader / BufferedReader的“读取”行为 - java InputStreamReader / BufferedReader “read” behavior Android:BufferedReader InputStreamReader返回旧值 - Android : BufferedReader InputStreamReader returning old value 使用InputStreamReader和BufferedReader读取HTTP响应的速度很慢? - Slow reading of HTTP response using InputStreamReader and BufferedReader? 释放由BufferedReader,InputStreamReader和InputStream获取的资源 - Releasing resources acquired by BufferedReader, InputStreamReader and InputStream 更好地使用ObjectOut / InputStream或InputStreamReader / BufferedReader? - Better to use ObjectOut/InputStream or InputStreamReader / BufferedReader?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM