简体   繁体   English

java阅读器与流

[英]java reader vs. stream

I was reading about Java I/O and found some interesting areas like streams, readers etc. 我正在阅读有关Java I / O的内容,并发现了一些有趣的领域,如流,读者等。

 InputStream input = new FileInputStream("input-file.txt");
 int data = input.read();
 while(data != -1){
   data = input.read();
 }

I can do the same thing by using Readers as follows: 我可以通过如下使用读者来做同样的事情:

Reader reader = new FileReader("input-file.txt");
 int data = reader.read();
 while(data != -1){
     char dataChar = (char) data;
     data = reader.read();
 }

As I know, Streams are used to retrieve input from continuously flowing data. 据我所知,Streams用于从连续流动的数据中检索输入。

Now I am confused with the difference between Streams & readers; 现在我对Streams和读者之间的区别感到困惑; and if we wraps the stream with a buffered reader - how it break lines, since stream is a continuously flowing thing. 如果我们用一个缓冲的阅读器包裹流 - 它如何打破线条,因为流是一个不断流动的东西。

I found some reference sites like this site. 我发现了一些像这个网站的参考网站。 But I can't understand the difference. 但我无法理解其中的区别。

Please can someone please help me to understand? 请有人帮我理解一下吗?

Readers are to read text data with particular character encoding (UTF-8, ISO etc..) 读者应阅读具有特定字符编码的文本数据(UTF-8,ISO等...)

while on the other hand, streams are binary data. 而另一方面,流是二进制数据。

They work same but there parent classes are different. 它们的工作方式相同,但父类不同。

in a nutshell, if you have to read binary data and save it somewhere, use stream. 简而言之,如果您必须读取二进制数据并将其保存在某处,请使用流。

If you have to read text in a particular encoding and then play with it, then use readers. 如果您必须阅读特定编码的文本然后使用它,请使用阅读器。

Hope this answers. 希望这个答案。

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

相关问题 Java - 从文件中读取。输入流与读者 - Java — reading from a file. Input stream vs. reader Java - 字节流与字符流? - Java - stream of bytes vs. stream of characters? 流对比 Map 的 entrySet 中的迭代器 - Java 8 - Stream Vs. Iterator in entrySet of a Map - Java 8 在Java Stream.of()与IntStream与Arrays.stream()生成的Stream上调用map(Arrays :: toString) - Calling map(Arrays::toString) on a Stream produced by Java Stream.of() vs. IntStream vs. Arrays.stream() Java 文件 stream 的 byte[] 与 char[] 的性能 - Java performance of byte[] vs. char[] for file stream 就内存,处理器,时间而言,BufferInputReader与LineNumberReader与Java Stream之间的最佳文件阅读器是什么? - What is Best File Reader in between BufferInputReader Vs LineNumberReader vs Stream in Java in terms of Memory, Processor, Time JDBC / WebRowSet:readXml(阅读器阅读器)与readXml(InputStream iStream) - JDBC / WebRowSet: readXml(Reader reader) vs. readXml(InputStream iStream) Java-Stream - 行为差异:空 stream 的 anyMatch 与 allMatch 和 noneMatch - Java-Stream - Difference in behaviour: anyMatch vs. allMatch and noneMatch for an empty stream Java:If vs. Switch - Java: If vs. Switch 使用-gc true在Java 12与Java 8上对流API进行神秘的微基准测试结果 - Mystifying microbenchmark result for stream API on Java 12 vs. Java 8 with -gc true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM