流的作用:如何從能夠發(fā)送字節(jié)序列的任何數(shù)據(jù)源取得輸入,以及如何將輸出發(fā)送到能夠接收字節(jié)序列的任何目的地。即:輸入/輸出問題。
字節(jié)序列的源和目的地可以是文件、網(wǎng)絡(luò)連接、內(nèi)存塊等,存儲在文件中的信息和從網(wǎng)絡(luò)連接中接收的信息,從本質(zhì)上處理方法是相同的。
數(shù)據(jù)最終都保存為一個字節(jié)序列,但是在程序設(shè)計中應(yīng)當用更高級的數(shù)據(jù)結(jié)構(gòu)來處理,如字符或?qū)ο笮蛄械取?/p>
java.io 包中定義了多個流類型(類或抽象類)來實現(xiàn)輸入/輸出功能;
可以從不同的角度對其進行分類:
按數(shù)據(jù) 流的方向 不同可以分為 輸入流 和 輸出流。(以程序的角度來考慮)
按處理數(shù)據(jù) 單位 不同可以分為 字節(jié)流 和 字符流。
按照 功能 不同可以分為 節(jié)點流 和 處理流。
J2SDK 所提供的所有流類型位于包java.io內(nèi)都分別繼承自以下四種抽象流類型。
節(jié)點流和處理流:
節(jié)點流為可以從一個特定的數(shù)據(jù)源(節(jié)點)讀寫數(shù)據(jù)(如:文件,內(nèi)存)
處理流是“連接”在已存在的流(節(jié)點流或處理流)之上,通過對數(shù)據(jù)的處理為程序提供更為強大的讀寫功能。
InputStream
繼承自InputSteam的流都是用于向程序中輸入數(shù)據(jù),且數(shù)據(jù)的單位為字節(jié)(8 bit);下圖中深色為節(jié)點流,淺色為處理流。
InputStream的基本方法
讀取一個字節(jié)并以整數(shù)的形式返回(0~255),
如果返回-1已到輸入流的末尾。
int read() throws IOException
讀取一系列字節(jié)并存儲到一個數(shù)組buffer,
返回實際讀取的字節(jié)數(shù),如果讀取前已到輸入流的末尾返回-1
int read(byte[] buffer) throws IOException
讀取length個字節(jié)并存儲到一個字節(jié)數(shù)組buffer,從off位置開始存,最多l(xiāng)en
返回實際讀取的字節(jié)數(shù),如果讀取前以到輸入流的末尾返回-1
int read(byte[] buffer, int off, int len)
throws IOException
關(guān)閉流釋放內(nèi)存資源
void close() throws IOException
OutputStream
繼承自O(shè)utputSteam的流是用于程序中輸入數(shù)據(jù),且數(shù)據(jù)的單位為字節(jié)(8 bit);下圖中深色為節(jié)點流,淺色為處理流。
OutputStream的基本方法
向輸出流中寫入一個字節(jié)數(shù)據(jù),該字節(jié)數(shù)據(jù)為參數(shù)b的低8位
void write(int b) throws IOException
將一個字節(jié)類型的數(shù)組中的數(shù)據(jù)寫入輸出流
void write(byte[] b) throws IOException
將一個字節(jié)類型的數(shù)組中的從指定位置(off)開始的len個字節(jié)寫入到輸出流
void write(byte[] b, int off, int len)
throws IOException
關(guān)閉流釋放內(nèi)存資源
void close() throws IOException
將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地
void flush() throws IOException
Reader
繼承自Reader的流都是用于向程序中輸入數(shù)據(jù),且數(shù)據(jù)的單位為字符(16 bit);下圖中深色為節(jié)點流,淺色的為處理流。
Reader 的基本方法
讀取一個字符并以整數(shù)的形式返回(0~255),如果返回-1已到輸入流的末尾。
int read() throws IOException
讀取一系列字符并存儲到一個數(shù)組buffer,返回實際讀取的字符數(shù),如果讀取前已到輸入流的末尾返回-1
int read(char[] cbuf) throws IOException
讀取length個字符并存儲到一個數(shù)組buffer,從off位置開始存,最多讀取len返回實際讀取的字符數(shù),如果讀取前以到輸入流的末尾返回-1
int read(char[] cbuf, int off, int len)
throws IOException
關(guān)閉流釋放內(nèi)存資源
void close() throws IOException
Writer
繼承自Writer的流都是用于程序中輸入數(shù)據(jù),且數(shù)據(jù)的單位為字符(16 bit);下圖中深色為節(jié)點流,淺色為處理流。
Writer 的基本方法
向輸出流中寫入一個字符數(shù)據(jù),該字節(jié)數(shù)據(jù)為參數(shù)b的低16位
void write(int c) throws IOException
將一個字符類型的數(shù)組中的數(shù)據(jù)寫入輸出流,
void write(char[] cbuf) throws IOException
將一個字符類型的數(shù)組中的從指定位置(offset)開始的length個字符寫入到輸出流
void write(char[] cbuf, int offset, int length)
throws IOException
將一個字符串中的字符寫入到輸出流
void write(String string) throws IOException
將一個字符串從offset開始的length個字符寫入到輸出流
void write(String string, int offset, int length)
throws IOException
關(guān)閉流釋放內(nèi)存資源
void close() throws IOException
將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地
void flush() throws IOException
節(jié)點流類型
訪問文件
FileInputStream和FileOutputStream分別繼承自InputStream和OutputStream用于向文件中輸入和輸出字節(jié)。
FileInputStream和FileOutputStream的常用構(gòu)造方法:
FileInputStream(String name) throws FileNotFoundException
FileInputStream(File file) throws FileNotFoundException
FileOutputStream(String name)throws FileNotFoundException
FileOutputStream(File file) throws FileNotFoundException
FileOutputStream(File file, boolean append)
throws FileNotFoundException
FileInputSteam 和 FileOutputStream 類支持其父類InputStream 和OutputStream 所提供的數(shù)據(jù)讀寫方法。
注意:
在實例化FileInputStream和FileOutputSteam流時要用try-catch語句以處理其可能拋出的FileNotFoundException。
在讀寫數(shù)據(jù)時也要用try-catch語句以處理可能拋出的 IOException。
FileNotFoundException是IOException的子類
FileReader 和 FileWriter 分別繼承自Reader和Writer,F(xiàn)ileInputSteam與FileOutputStream類似,所不同的是
FileReader和FileWriter向文件輸入和輸出的數(shù)據(jù)單位為字符。
FileReader和FileWriter的常用構(gòu)造方法:
public FileWriter(File file) throws IOException
public FileWriter(File file, boolean append)
throws IOException
public FileWriter(String fileName)throws IOException
public FileWriter(String fileName,boolean append)
throws IOException
public FileReader(String fileName)
throws FileNotFoundException
public FileReader(File file)
throws FileNotFoundException
處理流類型
緩沖流
緩沖流要“套接”在相應(yīng)的節(jié)點流之上,對讀寫的數(shù)據(jù)提供了緩沖的功能,提高了讀寫的效率,同時增加了一些新的方法。
J2SDK提供了四種緩存流,其常用的構(gòu)造方法為:
BufferedReader(Reader in)
BufferedReader(Reader in,int sz) //sz 為自定義緩存區(qū)的大小
BufferedWriter(Writer out)
BufferedWriter(Writer out,int sz)
BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in,int size)
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out,int size)
BufferedReader提供了readLine方法用于讀取一行字符串(以\r或\n分隔)。
BufferedWriter提供了newLine用于寫入一個行分隔符。
對于輸出的緩沖流,寫出的數(shù)據(jù)會先在內(nèi)存中緩存,使用flush方法將會使內(nèi)存中的數(shù)據(jù)立刻寫出。
轉(zhuǎn)換流
InputStreamReader和OutputStreamWriter用與字節(jié)數(shù)據(jù)到字符數(shù)據(jù)之間的轉(zhuǎn)換。
InputStreamReader 需要和 InputStream “套接” 。
OutpStreamWriter 需要和 OutputStream “套接” 。
轉(zhuǎn)換流在構(gòu)造時可以指定其編碼集合,例如:
InputStream isr = new InputStreamReader
(System.in, “ISO8859_1”)
數(shù)據(jù)流&ByteArrayInputStream&ByteArrayOutputStream
DataInputStream 和 DataOutputStream 分別繼承自InputSteam 和 OutputStream,它屬于處理流,需要分別“套接”在InputStream 和OutputStream類型的節(jié)點流上。
DataInputStream和DataOutputStream提供了可以存取與機器無關(guān)的Java原始類型數(shù)據(jù)(如:int,double 等)的方法。
DataInputStream和DataOutputStream的構(gòu)造方法為:
DataInputStream ( InputStream in )
DataOutputStream ( OutputStream out )
Print 流
PrintWriter和PrintStream 都屬于輸出流,分別針對與字符和字節(jié)。
PrintWriter和PrintStream提供了重載的print
Println方法用于多種數(shù)據(jù)類型的輸出。
PrintWriter和PrintStream的輸出操作不會拋出異常,用戶通過檢測錯誤狀態(tài)獲取錯誤信息。
PrintWriter和PrintStream有自動flush功能。
PrintWriter(Writer out)
PrintWriter(Writer out,boolean autoFlush)
PrintWriter(OutputStream out)
PrintWriter(OutputStream out,boolean autoFlush)
PrintStream(OutputStream out)
PrintStream(OutputStream out,booleanautoFlush)
Object流
直接將Object寫入或讀出
transient關(guān)鍵字
serializable接口
Externalizable 接口
void writeExternal(ObjectOutput out) throws IOException
void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
總結(jié)
InputStream/OutputStream //最基礎(chǔ)的抽象類,字節(jié)流
Reader/Writer //最基礎(chǔ)的抽象類 ,字符流
FileInputStream / FileOutputStream //File打頭的用來操作文件,字節(jié)流
FileReader / FileWriter //File打頭的用來操作文件,字符流
BufferedInputStream / BufferedOutputStream //帶緩沖的,字節(jié)流
BufferedReader / BufferedWriter //帶緩沖的,字符流
ByteArrayInputStream / ByteArrayOutputStream //讀寫內(nèi)存中的字符數(shù)組
InputStreamReader / OutputStreamWriter //字節(jié)轉(zhuǎn)字符,轉(zhuǎn)化流
DataInputStream / DataOutputStream //讀寫基本數(shù)據(jù)類型
PrintStream / PrintWriter //都是輸出流,不拋出異常,自動flush
ObjectInputStream / ObjectOutputStream //讀寫Object
Serializable接口?標記性接口
Externalizable ?自己控制序列化
Transient?
posted on 2010-11-05 18:02
Mineralwasser 閱讀(1152)
評論(1) 編輯 收藏