简体   繁体   English

Java流和阅读器之间有什么区别?

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

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. 谢谢。

An InputStream is byte-oriented. InputStream是面向字节的。 A Reader is character-oriented. 读者是面向角色的。

The javadocs are your friend, explaining the difference. javadocs是您的朋友,解释了两者之间的区别。 Reader , InputStream 阅读器为InputStream

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. 我之所以说得更好,是因为Reader(在典型用法的上下文中)本质上是具有易于促进阅读字符输入的方法的流。

Stream is for reading bytes, Reader is for reading characters. 流用于读取字节,读取器用于读取字符。 One character may take one byte or more, depending on character set. 一个字符可能占用一个字节或更多个字节,具体取决于字符集。

Stream classes are byte-oriented classes, that mean all InputStream classes (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. 流类是面向字节的类,这意味着所有InputStream类(缓冲和非缓冲)都从流中逐字节读取数据,而所有OutputStream (缓冲和非缓冲)都将字节逐字节地写入数据流。 Stream classes are useful when you have small data or if you are dealing with binary files like images. 当数据量较小或处理二进制文件(例如图像)时,流类很有用。

On the other hand Reader/Writer are character based classes. 另一方面, Reader/Writer是基于字符的类。 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). 这些类扩展了java.io.Reader (所有字符输入类)或java.io.Writer (所有字符输出类)。 These classes are useful if you are dealing with text file or other textual stream. 如果您要处理文本文件或其他文本流,则这些类很有用。 These classes are also Buffered and Non-Buffered . 这些类也是BufferedNon-Buffered

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

相关问题 java中InputStream和Reader之间的实际区别是什么 - What is actually difference between InputStream and Reader in java Java Stream - filter() 和 dropWhile() 有什么区别 - Java Stream - what is the difference between filter() and dropWhile() Reader 和 InputStream 有什么区别? - What is the difference between Reader and InputStream? 就内存,处理器,时间而言,BufferInputReader与LineNumberReader与Java Stream之间的最佳文件阅读器是什么? - What is Best File Reader in between BufferInputReader Vs LineNumberReader vs Stream in Java in terms of Memory, Processor, Time “节点流”和“过滤流”有什么区别? - What is the difference between a "node stream" and a "filter stream"? .stream()和Stream.of有什么区别? - What is the difference between .stream() and Stream.of? Java中&amp;和&amp;&amp;有什么区别? - What is the difference between & and && in Java? Java中的&gt; =和=&gt;有什么区别? - What is the difference between >= and => in Java? Java 中 ArrayList 上的 Stream.builder() 和调用 stream() 有什么区别? - What's the difference between Stream.builder() and calling stream() on an ArrayList in Java? 在Java 8 Stream API中,DirectoryStream有什么区别 <Path> 和流 <Path> ? - In Java 8 Stream API, what's the difference between a DirectoryStream<Path> and Stream<Path>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM