Java IO串流分析整理

來源:互聯網
上載者:User

標籤:

Java IO流的分類 Java中的流,可以從不同的角度進行分類。按流向分類:輸入資料流: 程式可以從中讀取資料的流。
輸出資料流: 程式能向其中寫入資料的流。按資料轉送單位分類:

位元組流:以位元組(8位二進位)為單位進行處理。主要用於讀寫諸像或聲音的位元據。

字元流:以字元(16位二進位)為單位進行處理。

都是通過位元組流的方式實現的。字元流是對位元組流進行了封裝,方便操作。在最底層,所有的輸入輸出都是位元組形式的。

尾碼是Stream是位元組流,而尾碼是Reader,Writer是字元流。

 

按功能分類:

節點流:從特定的地方讀寫的流類,如磁碟或者一塊記憶體地區。

過濾流:使用節點流作為輸入或輸出。過濾流是使用一個已經存在的輸入資料流或者輸出資料流串連建立的。

 

不同的輸入輸出資料流

JDK提供的流繼承了四大類:InputStream(位元組輸入資料流),OutputStream(位元組輸出資料流),Reader(字元輸入資料流),Writer(字元輸出資料流)

位元組輸入資料流:

位元組輸入資料流抽象類別及其關鍵的方法:類  InputStream
java.io.InputStreamabstract int read()
從輸入資料流中讀取資料的下一個位元組。
int read(byte[] b)
從輸入資料流中讀取一定數量的位元組,並將其儲存在緩衝區數組 b 中。
int read(byte[] b, int off, int len)
將輸入資料流中最多 len 個資料位元組讀入 byte 數組。 輸入資料流的操作: 開啟一個輸入資料流
迴圈讀取
關閉輸入資料流

這裡使用InputStream的子類FileInputStream讀入檔案:

public static void main(String[] args) throws IOException {    //建立檔案輸入資料流    InputStream is = new FileInputStream("D:/itzhai/arthinking.txt");    //建立位元組緩衝    byte[] buffer = new byte[100];    int length = 0;    //以位元組形式迴圈讀取檔案    while((length = is.read(buffer, 0, buffer.length)) != -1){        //把位元組轉換成字元並輸出        String str =new String(buffer, 0, length);        System.out.println(str);    }}
抽象類別InputStream的類層次:

位元組數組輸入資料流ByteArrayInputStream

把位元組數組作為源的輸入資料流。

相關例子:

位元組數組輸入資料流:

public static void main(String[] args) {    //建立讀取資料來源    String input = "arthinking";    //擷取位元組數組    byte[] b = input.getBytes();    //建立位元組數組輸出資料流    ByteArrayInputStream bis = new ByteArrayInputStream(b);    //迴圈逐個讀取    for(int i = 0; i < input.length(); i++){        int c;        //讀取下一個位元組        while((c = bis.read()) != -1){            System.out.print((char)c);        }    }    //將緩衝區的位置重設為標記位置    bis.reset();}

 

位元組數組輸出資料流:

public static void main(String[] args) throws IOException {    //建立位元組輸出資料流    ByteArrayOutputStream bos = new ByteArrayOutputStream();    String output = "arthinking";    //建立需要輸出的位元組數組    byte[] buffer = output.getBytes();    //把位元組數組寫到輸出資料流    bos.write(buffer);    //建立檔案輸出資料流    OutputStream os = new FileOutputStream("D:/itzhai/arthinking.txt");    //把位元組輸出資料流寫到檔案輸出資料流    bos.writeTo(os);}

  

 

位元組輸出資料流:

位元組輸出資料流的抽象類別及其最關鍵的方法:
類 OutputStreamjava.lang.Objectjava.io.OutputStreamvoid write(byte[] b)
將 b.length 個位元組從指定的 byte 數組寫入此輸出資料流。
void write(byte[] b, int off, int len)

 

將指定 byte 數組中從位移量 off 開始的 len 個位元組寫入此輸出資料流。
abstract void write(int b)
將指定的位元組寫入此輸出資料流。

由此可以看出,只有最後一個方法才是抽象的,原因是前面兩個都調用了第三個抽象方法,這樣繼承這個抽象類別的子類都必須提供抽象的write(int b)的實現,從而使得每個子類的實現都不一樣。

輸出資料流的操作: 開啟輸出資料流
迴圈寫入
關閉輸入資料流

這裡使用了OutputStream的子類FileOutputStream輸出到檔案:

public static void main(String[] args) throws IOException {    //建立一個輸出資料流    OutputStream os = new FileOutputStream("D:/itzhai/arthinking.txt", true);    String output = "http://www.itzhai.com";    //從字串中擷取位元組數組    byte[] buffer = output.getBytes();    //寫出到輸出資料流    os.write(buffer);    //關閉輸出資料流    os.close();}

  

抽象類別OutputStream的類階層:

過濾流:

過濾流不能直接跟檔案打交道,只能通過節點流進行相關的操作。可以從其構造方法中看出:

FilterOutputStream(OutputStream out)

需要傳入一個OutputStream。

在InputStream和OutputStream的子類中,

FilterInputStream和FilterOutputStream是過濾流,其又派生出子類DataInputStream和DataOutputStream資料輸入流和資料輸出資料流。

過濾流的主要特點是在輸入輸出資料同時對所傳輸的資料做指定類型或格式的轉換。

緩衝輸出資料流BufferedOutputStream

該類實現緩衝的輸出資料流。通過設定這種輸出資料流,應用程式就可以將各個位元組寫入底層輸出資料流中,而不必針對每次位元組寫入調用底層系統。

當緩衝區寫滿或者關閉輸出資料流時,一次性輸出到流,或者調用flush()方法主動將緩衝區輸出到流。

過濾流的使用例子:

使用過濾流類BufferedOutputStream和DataOutputStream裝飾FilterOutputStream的例子

public static void main(String[] args) throws IOException {    //建立資料輸出資料流    DataOutputStream dos = new DataOutputStream(            new BufferedOutputStream(new FileOutputStream("D:/itzhai/arthinking.txt")));    byte a = 1;    char b = ‘a‘;    int c = 2;    //使用資料輸出資料流對象的方法寫出資料到輸出資料流    dos.write(a);    dos.write(b);    dos.write(c);    //關閉資料輸出資料流    dos.close();    //建立資料輸入流    DataInputStream dis = new DataInputStream(            new BufferedInputStream(new FileInputStream("D:/itzhai/arthinking.txt")));    //使用資料輸入流的方法從輸入資料流中讀取資料    System.out.println(dis.readByte() + dis.readChar() + dis.readInt());    //關閉資料輸入流    dis.close();}

  

使用DataInputStream和DataOutputStream資料檔案流的一般步驟:

  • 建立位元組檔案流對象
  • 基於位元組檔案流對象建立資料檔案流對象
  • 用資料檔案流對象的方法對基本類型的資料進行輸入/輸出 
字元輸入資料流:

字元輸出資料流:

 

Java IO串流分析整理

聯繫我們

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