大部分內(nèi)容來自 The Java? Tutorials 官方指南况鸣,其余來自別處如ifeve的譯文余舶、imooc、書籍Android面試寶典等等。
作者: @youyuge
個(gè)人博客站點(diǎn): https://youyuge.cn
一逗堵、索引
概述 Basic I/O (即本文第二部分)
二、概述 Basic I/O
2.1 Java.io 包的范圍
java.io
包并沒有涵蓋所有輸入輸出類型。例如寿谴,并不包含GUI或者網(wǎng)頁上的輸入輸出,這些輸入和輸出在其它地方都涉及失受,比如Swing工程中的JFC (Java Foundation Classes) 類讶泰,或者J2EE里的Servlet和HTTP包。
Java.io
包主要涉及文件拂到,網(wǎng)絡(luò)數(shù)據(jù)流痪署,內(nèi)存緩沖等的輸入輸出。
2.2 Java I/O的定義
Java IO 是一套Java用來讀寫數(shù)據(jù)(輸入和輸出)的API兄旬。
2.3 I/O流的定義
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.
一個(gè)I/O流代表了一個(gè)輸入源或者一個(gè)輸出點(diǎn)狼犯。一個(gè)流能代表了多種源或目的點(diǎn),包括從硬盤、設(shè)備辜王、其他設(shè)備或者內(nèi)存數(shù)組劈狐。
注意:流代表了數(shù)據(jù)流(data flow),強(qiáng)調(diào)的是流動(dòng)的方向呐馆,而非代表數(shù)據(jù)本身肥缔。
2.4 I/O流簡介
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.
流提供了傳輸多種數(shù)據(jù)的途徑,包括字節(jié)byte汹来,原始數(shù)據(jù)類型续膳,字符和對象。一些流只是簡單地傳遞數(shù)據(jù)收班,另一些則操作或轉(zhuǎn)換數(shù)據(jù)成有用的形式坟岔。
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:
不論如何這些流內(nèi)部如何工作,所有的流都作為數(shù)據(jù)的序列這種簡單的模型摔桦,來展現(xiàn)給使用它們的程序社付。一個(gè)程序使用輸入流,從源(外部)讀取數(shù)據(jù)到我們的Java程序中邻耕,一下一個(gè):
同理鸥咖,使用輸出流,寫數(shù)據(jù)到目的地:
注意:這里的輸入兄世、輸出都是針對我們的Java程序而言的啼辣,輸入即從源讀取信息到我們的Java程序中,輸出即輸出信息到外部目的地(如文件)御滩。
不是針對一個(gè)文件而言的輸入和輸出鸥拧,流的概念重點(diǎn)是在源和目的點(diǎn),可以把Java程序看成是一個(gè)中介削解。
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.
上述圖片中的數(shù)據(jù)源與數(shù)據(jù)目的地可以是持有富弦、生成或消費(fèi)數(shù)據(jù)的任何東西。很明顯钠绍,常見的是硬盤文件舆声,但是也能是另一個(gè)程序,一個(gè)外部設(shè)備柳爽、一個(gè)網(wǎng)絡(luò)套接字socket(socket網(wǎng)絡(luò)編程中流的概念非常重要)或者一個(gè)數(shù)組。
三碱屁、I/O流的分類
3.1 字節(jié)流的分類
3.2 字符流的分類
了解了基本的概念磷脯,可以進(jìn)一步深入了解字節(jié)流、字符流等等了娩脾。索引在本文最上面??