Java Stream流的概念

來源:互聯網
上載者:User

關於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.

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

 

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 (FileInputStream, ServletInputStream, 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 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.