且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是 InputStream &输出流?我们为什么以及何时使用它们?

更新时间:2022-06-12 06:05:48

InputStreamOutputStream 的目标是抽象不同的输入输出方式:是文件、网页还是屏幕应该无关紧要.重要的是您从流中接收信息(或将信息发送到流中.)

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 instrOutputStream 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();