Java 中的流和读取器有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2428076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:06:03  来源:igfitidea点击:

What is the difference between a stream and a reader in Java?

javastream

提问by bragboy

Today I got this question for which I think I answered very bad. I said stream is a data that flows and reader is a technique where we read from that is a static data. I know this is an awful answer, so please provide me the crisp difference and definitions between these two with example in Java.

今天我得到了这个问题,我认为我的回答很糟糕。我说流是一种流动的数据,而阅读器是一种我们从中读取静态数据的技术。我知道这是一个糟糕的答案,所以请以 Java 中的示例为我提供这两者之间的明确区别和定义。

Thanks.

谢谢。

采纳答案by Tom Neyland

As others have said, the use cases for each are slightly different (even though they often can be used interchangeably)

正如其他人所说,每个用例的用例略有不同(即使它们通常可以互换使用)

Since readers are for reading characters, they are better when you are dealing with input that is of a textual nature (or data represented as characters). I say better because Readers (in the context of typical usage) are essentially streams with methods that easily facilitate reading character input.

由于读取器用于读取字符,因此当您处理具有文本性质的输入(或表示为字符的数据)时,它们会更好。我说得更好,因为 Readers(在典型用法的上下文中)本质上是具有易于读取字符输入的方法的流。

回答by brabster

An InputStream is byte-oriented. A Reader is character-oriented.

InputStream 是面向字节的。Reader 是面向字符的。

The javadocs are your friend, explaining the difference. Reader, InputStream

javadocs 是你的朋友,解释了不同之处。阅读器为InputStream

回答by Peter ?tibrany

Stream is for reading bytes, Reader is for reading characters. One character may take one byte or more, depending on character set.

Stream 用于读取字节,Reader 用于读取字符。一个字符可能占用一个字节或更多字节,具体取决于字符集。

回答by Sachin Kumar

Stream classes are byte-oriented classes, that mean all InputStreamclasses (Buffered and non-buffered) read data byte by byte from stream and all OutputStream(Buffered and non-buffered) classes writes data byte by byte to the stream. Stream classes are useful when you have small data or if you are dealing with binary files like images.

流类是面向字节的类,这意味着所有InputStream类(缓冲和非缓冲)从流中逐字节读取数据,所有OutputStream(缓冲和非缓冲)类将数据逐字节写入流。当您有小数据或处理像图像这样的二进制文件时,流类很有用。

On the other handReader/Writerare character based classes. These classes read or write one character at time from or into stream. These classes extends either java.io.Reader(all character input classes) or java.io.Writer(all character output classes). These classes are useful if you are dealing with text file or other textual stream. These classes are also Bufferedand Non-Buffered.

另一方面Reader/Writer是基于字符的类。这些类一次从流中读取或写入一个字符。这些类扩展java.io.Reader(所有字符输入类)或java.io.Writer(所有字符输出类)。如果您正在处理文本文件或其他文本流,这些类很有用。这些类也是BufferedNon-Buffered