InputStream与InputStreamReader(InputStream vs InputStreamReader)
在
InputStreamReader
上使用InputStream
有什么好处,反之亦然。以下是
InputStream
一个示例:InputStream input = new FileInputStream("c:\\data\\input-text.txt"); int data = input.read(); while(data != -1) { //do something with data... doSomethingWithData(data); data = input.read(); } input.close();
这里是一个使用InputStreamReader的例子(显然在InputStream的帮助下):
InputStream inputStream = new FileInputStream("c:\\data\\input.txt"); Reader reader = new InputStreamReader(inputStream); int data = reader.read(); while(data != -1){ char theChar = (char) data; data = reader.read(); } reader.close();
读者是否以特殊的方式处理数据?
只是试图让我的头脑围绕Java中的整个
i/o
流数据方面。What's the benefit of using
InputStream
overInputStreamReader
, or vice versa.Here is an example of
InputStream
in action:InputStream input = new FileInputStream("c:\\data\\input-text.txt"); int data = input.read(); while(data != -1) { //do something with data... doSomethingWithData(data); data = input.read(); } input.close();
And here is an example of using InputStreamReader (obviously with the help of InputStream):
InputStream inputStream = new FileInputStream("c:\\data\\input.txt"); Reader reader = new InputStreamReader(inputStream); int data = reader.read(); while(data != -1){ char theChar = (char) data; data = reader.read(); } reader.close();
Does the Reader process the data in a special way?
Just trying to get my head around the whole
i/o
streaming data aspect in Java.
原文:https://stackoverflow.com/questions/3194918
更新时间:2023-04-09 22:04