關於Java IO流學習總結

來源:互聯網
上載者:User
摘自: www.chinaitlab.com  被閱讀次數: 18

由 yangyi 於 2009-03-03 22:00:02 提供

    一、IO流的三種分類方式

    1.按流的方向分為:輸入資料流和輸出資料流

    2.按流的資料單位不同分為:位元組流和字元流

    3.按流的功能不同分為:節點流和處理流

    二、IO流的四大抽象類別:

    字元流:Reader Writer

    位元組流:InputStream(讀資料)

    OutputStream(寫資料)

    三、InputStream的基本方法

    int read() throws IOException 讀取一個位元組以整數形式返回,如果返回-1已到輸入資料流的末尾

    void close() throws IOException 關閉流釋放記憶體資源

    long skip(long n) throws IOException 跳過n個位元組不讀

    四、OutputStream的基本方法

    void write(int b) throws IOException 向輸出資料流寫入一個位元組資料

    void flush() throws IOException 將輸出資料流中緩衝的資料全部寫出到目的地

    五、Writer的基本方法

    void write(int c) throws IOException 向輸出資料流寫入一個字元資料

    void write(String str) throws IOException將一個字串中的字元寫入到輸出資料流

    void write(String str,int offset,int length)

    將一個字串從offset開始的length個字元寫入到輸出資料流

    void flush() throws IOException

    將輸出資料流中緩衝的資料全部寫出到目的地

    六、Reader的基本方法

    int read() throws IOException 讀取一個字元以整數形式返回,如果返回-1已到輸入資料流的末尾

    七、節點流類型

    八、訪問檔案之FileInputStream和FileOutputStream繼承基類用於向檔案中輸入輸出位元組

    九、訪問檔案之FileReader和FileWriter繼承基類用於向檔案中輸入輸出字元

----輸出資料流在建構函式第二個參數可以設定true意義為跟在已有檔案後進行輸入
----此類流會拋出FileNotFoundException需要對其進行顯示捕捉

 

    十、緩衝流:緩衝流要套接在相應的節點流之上,提高了讀寫的效率。

    此處理流的構造方法都得傳相對應的基類類型

    BufferedReader:提供了readLine方法用於高校讀取一行字串

    BufferedWriter:提供了newLine用於寫入一個行分隔字元也就是換行

    BufferedInputStream 沒多大用處

    BufferedOutputStream 沒多大用處

    十一、轉換流:主要作用將位元組流轉換成字元流。用處較大!

    轉換流在構造時可以指定其編碼集合

    InputStreamReader需要和InputStream套接

    OutputStreamWriter需要和OutputStream套接

    例:OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream(檔案路徑);

    方法例:osw.getEncoding(); 獲得流的編碼方式

    十二、資料流與位元組數組流:

    資料流主要為實現可以存取Java未經處理資料類型如long,boolean

    資料流是位元組流

    DataInputStream需要和InputStream套接

    DataOutputStream需要和OutputStream套接

    DataInputStream方法:readBoolean() readInt() read……()……

    readUTF():網路傳輸常用方法 讀一個Unicode字串

    DataOutputStream方法與DataInputStream基本對應為寫的方法

    //此建構函式等於已可以往一個位元組數組裡輸入內容

    ByteArrayOutputStream baos = new ByteArrayOutputStream ();

    //此方法為擷取一個位元組數組方法返回位元組數組

    baos.toByteArray();

    //此方法擷取位元組數組佔了多少位元組

    new ByteArrayInputStream(一個位元組數組)。available()

 1ByteArrayOutputStream baos = 
 2                        new ByteArrayOutputStream(); 
 3    DataOutputStream dos = 
 4                        new DataOutputStream(baos);
 5{
function onclick()
{
this.style.display='none'; Codehighlighter1_172_558_Open_Text.style.display='none'; Codehighlighter1_172_558_Closed_Image.style.display='inline'; Codehighlighter1_172_558_Closed_Text.style.display='inline';
}
}" src="http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505997.gif" alt="" align="top">{
function onclick()
{
this.style.display='none'; Codehighlighter1_172_558_Closed_Text.style.display='none'; Codehighlighter1_172_558_Open_Image.style.display='inline'; Codehighlighter1_172_558_Open_Text.style.display='inline';
}
}" src="http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505279.gif" alt="" align="top">    try {
 6      dos.writeDouble(Math.random());
 7      dos.writeBoolean(true);
 8      ByteArrayInputStream bais = 
 9          new ByteArrayInputStream(baos.toByteArray());
10      System.out.println(bais.available());
11      DataInputStream dis = new DataInputStream(bais);
12      System.out.println(dis.readDouble());
13      System.out.println(dis.readBoolean());
14      dos.close();  dis.close();
15{
function onclick()
{
this.style.display='none'; Codehighlighter1_582_615_Open_Text.style.display='none'; Codehighlighter1_582_615_Closed_Image.style.display='inline'; Codehighlighter1_582_615_Closed_Text.style.display='inline';
}
}" src="http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505997.gif" alt="" align="top">{
function onclick()
{
this.style.display='none'; Codehighlighter1_582_615_Closed_Text.style.display='none'; Codehighlighter1_582_615_Open_Image.style.display='inline'; Codehighlighter1_582_615_Open_Text.style.display='inline';
}
}" src="http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505279.gif" alt="" align="top">    } catch (IOException e) {
16      e.printStackTrace();
17    }

    十二、Print流

    Print流只有輸出資料流無輸入資料流,PrintWriter和PrintStream分別針對字元位元組

    兩個類提供了重載的Print和Println方法用於多種資料類型的輸出

    PrintWriter和PrintStream的輸出操作不會拋出異常

    PrintWriter和PrintStream有自動flush功能

    ----System.setOut(接收OutputStream類):用於設定系統預設輸出資料流

    十二、Object流

    等同於c#序列化,用直接將Object寫入或讀出

    transient關鍵字為不序列化此成員變數

    需要序列化的類必須實現Serializable介面

    主要方法:writeObject(Object); readObject();

    讀出為Object類型需要強轉資料類型

 1 import java.io.*;
 2 
 3 public class TestObjectIO {
 4     public static void main(String args[]) throws Exception {
 5         T t = new T();
 6         t.k = 8;
 7         FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
 8         ObjectOutputStream oos = new ObjectOutputStream(fos);
 9         oos.writeObject(t);
10         oos.flush();
11         oos.close();
12         
13         FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
14         ObjectInputStream ois = new ObjectInputStream(fis);
15         T tReaded = (T)ois.readObject();
16         System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
17         
18     }
19 }
20 
21 class T 
22     implements Serializable
23 {
24     int i = 10;
25     int j = 9;
26     double d = 2.3;
27     transient int k = 15;
28 }

 

原文連結: http://java.chinaitlab.com/base/...

最後補充點:

一. Input和Output
1. stream代表的是任何有能力產出資料的資料來源,或是任何有能力接收資料的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括兩種類型:
1.1 以位元組為導向的stream
以位元組為導向的stream,表示以位元組為單位從stream中讀取或往stream中寫入資訊。以位元組為導向的stream包括下面幾種類型:
1) input stream:
1) ByteArrayInputStream:把記憶體中的一個緩衝區作為InputStream使用
2) StringBufferInputStream:把一個String對象作為InputStream
3) FileInputStream:把一個檔案作為InputStream,實現對檔案的讀取操作
4) PipedInputStream:實現了pipe的概念,主要線上程中使用
5) SequenceInputStream:把多個InputStream合并為一個InputStream
2) Out stream
1) ByteArrayOutputStream:把資訊存入記憶體中的一個緩衝區中
2) FileOutputStream:把資訊存入檔案中
3) PipedOutputStream:實現了pipe的概念,主要線上程中使用
4) SequenceOutputStream:把多個OutStream合并為一個OutStream
1.2 以Unicode字元為導向的stream
以Unicode字元為導向的stream,表示以Unicode字元為單位從stream中讀取或往stream中寫入資訊。以Unicode字元為導向的stream包括下面幾種類型:
1) Input Stream
1) CharArrayReader:與ByteArrayInputStream對應
2) StringReader:與StringBufferInputStream對應
3) FileReader:與FileInputStream對應
4) PipedReader:與PipedInputStream對應
2) Out Stream
1) CharArrayWrite:與ByteArrayOutputStream對應
2) StringWrite:無與之對應的以位元組為導向的stream
3) FileWrite:與FileOutputStream對應
4) PipedWrite:與PipedOutputStream對應

以字元為導向的stream基本上對有與之相對應的以位元組為導向的stream。兩個對應類實現的功能相同,字是在操作時的導向不同。如 CharArrayReader:和ByteArrayInputStream的作用都是把記憶體中的一個緩衝區作為InputStream使用,所不同的是前者每次從記憶體中讀取一個位元組的資訊,而後者每次從記憶體中讀取一個字元。
1.3 兩種不現導向的stream之間的轉換
InputStreamReader和OutputStreamReader:把一個以位元組為導向的stream轉換成一個以字元為導向的stream。
2. stream添加屬性
2.1 “為stream添加屬性”的作用
運用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個例子來說明這種功能的作用。
如果我們要往一個檔案中寫入資料,我們可以這樣操作:
FileOutStream fs = new FileOutStream(“test.txt”);
然後就可以通過產生的fs對象調用write()函數來往test.txt檔案中寫入資料了。但是,如果我們想實現“先把要寫入檔案的資料先緩衝到記憶體中,再把緩衝中的資料寫入檔案中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和 FilterOutStream的子類,為FileOutStream添加我們所需要的功能。
2.2 FilterInputStream的各種類型
2.2.1 用於封裝以位元組為導向的InputStream
1) DataInputStream:從stream中讀取基本類型(int、char等)資料。
2) BufferedInputStream:使用緩衝區
3) LineNumberInputStream:會記錄input stream內的行數,然後可以調用getLineNumber()和setLineNumber(int)
4) PushbackInputStream:很少用到,一般用於編譯器開發
2.2.2 用於封裝以字元為導向的InputStream
1) 沒有與DataInputStream對應的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream
2) BufferedReader:與BufferedInputStream對應
3) LineNumberReader:與LineNumberInputStream對應
4) PushBackReader:與PushbackInputStream對應
2.3 FilterOutStream的各種類型
2.2.3 用於封裝以位元組為導向的OutputStream
1) DataIOutStream:往stream中輸出基本類型(int、char等)資料。
2) BufferedOutStream:使用緩衝區
3) PrintStream:產生格式化輸出
2.2.4 用於封裝以字元為導向的OutputStream
1) BufferedWrite:與對應
2) PrintWrite:與對應
3. RandomAccessFile
1) 可通過RandomAccessFile對象完成對檔案的讀寫操作
2) 在產生一個對象時,可指明要開啟的檔案的性質:r,唯讀;w,唯寫;rw可讀寫
3) 可以直接跳到檔案中指定的位置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.