首頁 / CompScience / Programming / Languages / java / 鏈結串流 vs 連結串流

鏈結串流 vs 連結串流

鏈結串流可以單獨使用或與連結串流併用。這是ㄚ琪在看是深入淺出 Java 程式設計 第二版第十四章的序列話語檔案輸出入時碰到的問題,一開始ㄚ琪有點傻眼,鏈跟連一般不是都是一樣的意義嗎?怎會出現這樣的問題,當然如果課本會這樣出題的話,表示這兩個字確實是有分別的,可是ㄚ琪不清楚這裡的差異,翻開英文版的來看,『Chain streams can be used on their own or with connection streams.』Chain streams譯成鍊結串流,connection streams譯成連結串流,單獨看這個英文還是不懂,還真讓人苦惱。

而且Google的時候大部分都是跟這本書有關的資料才有Chain stream,所以讓ㄚ琪繼續看看,『The I/O uses the modular ‘chaining’ concept so that you can hook together connection streams and chain
streams (also called ‘filter’ streams) In a wide range of combi nations to get Just about anyth Ing you couId want .』在中文p.459沒有蠢問題這裡,第一個問題,『我查API的時候發現java.io這個package中有五百萬種class,你到底是怎麼知道要用哪一種?』

答案第一段,『I/O的API使用一種模組化的鏈接概念來讓你可以把連接串流與鏈接串流以各種可能應用到的排列組合連接起來。』注意,英文有這樣一句chain streams (also called ‘filter’ streams),這是中文沒有翻出的, Google到這樣一段簡報,Java的「過濾串流」()是在串流間加上過濾器,以便將上方串流的資料在處理後,才送到下方的串流,如下圖所示:

image

把矛頭對準在過濾串流看來是對的,再追溯到中文課本的p433,有解釋資料在串流中移動,英文是這樣『

The I/O API has amnection streams, that represent connections to destinations and
sources such as files or , and chain streams that work only if chained to
other streams.
Often, it takes at least two streams hooked together to do something useful-one to
represent the connection and anotherto call methods on. \\Thy two? Because connection
streams are usually too low-level. FileOutputStream (a ), for example,
has methods for writing byles. But we don’t want to write byusl We want to write objects, so
we need a higher-level .
OK, then why not have just a single stream that does exactly what you want? One that lets
you write objects but underneath converts them to bytes? Think good 00. Each class
does onething well. File OurpurStreams write bytes to a file. ObjectOurputStreams turn
objects into data that can be written to a stream. So we make a FileOutputStream that lets
us write to a file, and we hook an ObjectOutputStrearn (a chain stream) on the end of it.
When we call writeObject() on the ObjectOutputStream. the object gets pumped into the
stream and then moves to the FileOutputStream where it ultimately gets written as bytes
to a file.
The ability to mix and match different combinations of connection and chain streams
gives you tremendous flexibilityl Ifyou were forced to use only a singlestream class, you’d
be at the mercy of the APIdesigners. hoping they’d thought of everythitlgyou might ever
want to do. But with chaining, you can patch together your own custom chains.

2011-12-15_164305

看了圖之後,更懂了,chain stream應該叫做filter stream真的比較容易懂,我的空氣又再度新鮮起來了。

Print Friendly, PDF & Email
馬上成為工作達人的Fans

About ㄚ琪

工作達人Fun Taiwan的創辦者及總編,可以在這裡更認識他。

發表迴響

你的電子郵件位址並不會被公開。 Required fields are marked *

*

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

Scroll To Top