Java Stream流的概念

关于java中stream的概念,让人困惑了很久,下面是今天从网上摘抄的集锦:


刚开始接触流的概念,很迷惑,搞了很久终于搞懂,时隔许久,又迷惑了,现做一小结,拿来与大家分享。

        1. Java的流分为 Inputstream 和 OutputStream;

        2. 流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备、外部文件等;

        3. Java中的流,简单的说就是字节(byte),可以把它看作是很多很多字节(byte) 汇在一起形成的东西,起个名字就叫流,像水滴形成河流一样,呵呵,个人的解释;

        4. 一个流,必有源端和目的端,也即必须有数据(Data)和字节(byte)两个部分,数据(Data):可以是计算机内存的某些区域,也可以是磁盘文件,甚至可以是Internet上的某个URL;字节就不用说了 byte;

        5. 流的源端和目的端可简单地看成是字节的生产者和消费者,由数据变成字节,是生产字节、生产流;由字节恢复成数据,是读取字节、消费流;

        6. 流的方向很重要,根据流的方向,流可分为两类:输入流和输出流。流的方向是最难理解和最容易搞糊涂的

来源:http://www.blogjava.net/myfly/archive/2008/08/28/225275.html


以下是来自oracle的官方文档:

I/O Streams

An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time:

Reading information into a program.

Reading information into a program.

A program uses an output stream to write data to a destination, one item at time:

Writing information from a program.

Writing information from a program.

In this lesson, we'll see streams that can handle all kinds of data, from primitive values to advanced objects.

The data source and data destination pictured above can be anything that holds, generates, or consumes data. Obviously this includes disk files, but a source or destination can also be another program, a peripheral device, a network socket, or an array.

来自:http://docs.oracle.com/javase/tutorial/essential/io/streams.html


下面是某个论坛上一个比较通俗的解释:

A stream is data that you access in sequence. You could think of it like a train that you watch from a tunnel entrance so that you can just see one car at a time. Or a stream of widgets coming across a conveyor belt requiring you to tighten a screw on each one before it passes by to the next person down the assembly line who has to pound it with a hammer, and so on. Or sticks floating down a river while you watch from a bridge. 

InputStream and OutputStream are the basic stream classes in Java, one letting us bring data in from somewhere, the other letting us put data out to somewhere. All the other streams just add capabilities to the basics, like the ability to read a whole chunk of data at once for performance reasons (BufferedInputStream) or convert from one kind of character set to Java's native unicode (Reader), or say where the data is coming from (FileInputStreamServletInputStream, SocketInputStream, ByteArrayInputStream, etc.) 

The various classes are meant to wrap each other, so we can build, for example, a BufferedInputStream that reads from a file: 

InputStream in = new BufferedInputStream(new FileInputStream(myfile)); 

Then if you decide you want to read data from a network socket instead, you can just change FileInputStream() to SocketInputStream() (and add a bit of code to establish the network connection of course) and you won't have to change anything else in your program.

来自:http://www.coderanch.com/t/527192//java/stream-java 

原文地址:https://www.cnblogs.com/beanmoon/p/2924047.html