[英]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.