什么是 InputStream &输出流?我们为什么以及何时使用它们? [英] What is InputStream & Output Stream? Why and when do we use them?
问题描述
有人给我解释一下什么是InputStream
和OutputStream
?
Someone explain to me what InputStream
and OutputStream
are?
我对 InputStream
和 OutputStream
的用例感到困惑.
I am confused about the use cases for both InputStream
and OutputStream
.
如果您还可以包含一段代码来配合您的解释,那就太好了.谢谢!
If you could also include a snippet of code to go along with your explanation, that would be great. Thanks!
推荐答案
InputStream
和 OutputStream
的目标是抽象不同的输入输出方式:是文件、网页还是屏幕应该无关紧要.重要的是您从流中接收信息(或将信息发送到流中.)
The goal of InputStream
and OutputStream
is to abstract different ways to input and output: whether the stream is a file, a web page, or the screen shouldn't matter. All that matters is that you receive information from the stream (or send information into that stream.)
InputStream
用于您读取的许多内容.
InputStream
is used for many things that you read from.
OutputStream
用于您写入的许多内容.
OutputStream
is used for many things that you write to.
这是一些示例代码.它假设 InputStream instr
和 OutputStream osstr
已经创建:
Here's some sample code. It assumes the InputStream instr
and OutputStream osstr
have already been created:
int i;
while ((i = instr.read()) != -1) {
osstr.write(i);
}
instr.close();
osstr.close();
这篇关于什么是 InputStream &输出流?我们为什么以及何时使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!