问题描述
有人向我解释 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&输出流?我们为何以及何时使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!