java阅读器与流 [英] java reader vs. stream

查看:100
本文介绍了java阅读器与流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Java I / O的内容,并发现了一些有趣的领域,如流,读者等。

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

我可以通过如下使用读者来做同样的事情:

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

据我所知,Streams用于从连续流动的数据中检索输入。 / p>

现在我对Streams&和读者;如果我们用缓冲的阅读器包装流 - 它是如何打破线条的,因为流是一个不断流动的东西。



我找到了一些参考网站,如这个网站。但我无法理解其中的差异。



请有人可以帮我理解吗?

解决方案

读者要读取具有特定字符编码的文本数据( UTF-8,ISO等..)



而另一方面,流是二进制数据。



它们的工作方式相同,但父类不同。



简而言之,如果您必须读取二进制数据并将其保存在某处,请使用流。



如果你必须阅读特定编码的文本,然后使用它,然后使用读者。



希望这个答案。


I was reading about Java I/O and found some interesting areas like streams, readers etc.

 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.

Now I am confused with the difference between Streams & readers; 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..)

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.

这篇关于java阅读器与流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆