Java IO學習筆記四

內(nèi)存操作流

之前的所有的流操作都是針對文件的,但是有時候只是想要實現(xiàn)數(shù)據(jù)間轉(zhuǎn)換,此時如果我們想要創(chuàng)建一個文件然后再刪除文件,那樣顯得有點麻煩,因此此時的內(nèi)存操作流就顯得很適合這類的操作,因為它只是在內(nèi)存中存儲,并不會真正的創(chuàng)建文件,內(nèi)存操作流涉及的兩個類是ByteArrayInputStream,ByteArrayOutputStream.

ByteArrayInputStream

  • ByteArrayInputStream 包含一個內(nèi)部緩沖區(qū),該緩沖區(qū)包含從流中讀取的字節(jié)。內(nèi)部計數(shù)器跟蹤read方法要提供的下一個字節(jié)。
  • 關(guān)閉 ByteArrayInputStream無效。此類中的方法在關(guān)閉此流后仍可被調(diào)用,而不會產(chǎn)生任何 IOException
  • 主要的功能是從緩沖區(qū)讀取字節(jié)

構(gòu)造函數(shù)

  • ByteArrayInputStream(byte[] buf) 創(chuàng)建一個 ByteArrayInputStream,使用 buf 作為其緩沖區(qū)數(shù)組。
  • ByteArrayInputStream(byte[] buf, int offset, int length) 創(chuàng)建 ByteArrayInputStream,使用 buf 作為其緩沖區(qū)數(shù)組。

常用的方法

  • close() 不過對這個無效,因為關(guān)閉之后仍然可以使用函數(shù)讀取而不報錯
  • int read() 從緩沖區(qū)中讀取一個字節(jié)
  • int read(byte[] bytes) 將緩沖區(qū)中的內(nèi)容讀取到數(shù)組中
  • int read(byte[] bytes,int off,int len) 將最多 len 個數(shù)據(jù)字節(jié)從此輸入流讀入 byte 數(shù)組。
  • long skip(long n) 從此輸入流中跳過n 個輸入字節(jié)。
  • void reset() 將此 byte 數(shù)組輸出流的 count 字段重置為零,從而丟棄輸出流中目前已累積的所有輸出(清除緩沖區(qū))

實例

public class demo8 {
    public static void main(String args[]) {
        String str = "chenjiabing\n陳加兵";
        byte[] bytes = str.getBytes();  //創(chuàng)建一個數(shù)組
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);  //使用bytes作為緩沖區(qū)數(shù)組
        int temp = 0;
        /*第一種方法讀取緩沖區(qū)中的數(shù)據(jù),這個和文件的操作不一樣,這個可以直接沖緩沖區(qū)中讀取數(shù)據(jù)字節(jié)*/
        while ((temp = inputStream.read()) != -1) {
            System.out.print((char) temp);
        }

        /*創(chuàng)建數(shù)組用于存儲讀取的內(nèi)容,下面是第二種讀取數(shù)據(jù)的方法*/
        byte[] b = new byte[bytes.length];
        try {
            int len = inputStream.read(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(new String(b));

    }

}

ByteArrayOutputStream

  • 此類實現(xiàn)了一個輸出流,其中的數(shù)據(jù)被寫入一個byte 數(shù)組。緩沖區(qū)會隨著數(shù)據(jù)的不斷寫入而自動增長。可使用 toByteArray() 和 toString() 獲取數(shù)據(jù)。
  • 關(guān)閉 ByteArrayOutputStream 無效。此類中的方法在關(guān)閉此流后仍可被調(diào)用,而不會產(chǎn)生任何 IOException

構(gòu)造函數(shù)

  • ByteArrayOutputStream() 創(chuàng)建一個新的 byte數(shù)組輸出流。
  • ByteArrayOutputStream(int size) 創(chuàng)建一個新的 byte 數(shù)組輸出流,它具有指定大小的緩沖區(qū)容量(以字節(jié)為單位)。

常用函數(shù)

  • int size() 返回緩沖區(qū)的當前大小。
  • byte[] toByteArray() 創(chuàng)建一個新分配的 byte 數(shù)組。
  • String toString() 將緩沖區(qū)的字節(jié)轉(zhuǎn)換成字符串
  • void write(byte[] b, int off, int len) 將指定 byte 數(shù)組中從偏移量 off 開始的 len 個字節(jié)寫入此 byte 數(shù)組輸出流。
  • void write(int b) 將指定的字節(jié)寫入此 byte數(shù)組輸出流。

實例

public class demo8 {
    public static void main(String args[]) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String str = "chenjiabing";
        try {
            outputStream.write(str.getBytes());   //將字符串轉(zhuǎn)換成數(shù)組然后寫入緩沖區(qū)
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();  //這里的關(guān)閉無效
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //將緩沖區(qū)的數(shù)據(jù)轉(zhuǎn)換成字符串后輸出,這里同樣可以看出輸出流的關(guān)閉根本不影響函數(shù)的調(diào)用
        System.out.println(outputStream.size());  //輸出緩沖區(qū)的大小
        System.out.println(outputStream.toString());  //輸出chenjiabing
        outputStream.reset();   //清除緩沖區(qū)的內(nèi)容,如果不清零那么原先寫入的數(shù)據(jù)還是存在的,但是此時我們已經(jīng)不需要前面的數(shù)據(jù)了
        try {
            outputStream.write("陳加兵".getBytes());  //繼續(xù)向緩沖區(qū)寫入數(shù)據(jù)
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(outputStream.size());  //這里的一個漢字占了三個字節(jié)
        System.out.println(outputStream.toString());//輸出陳加兵


    }

}

綜合

下面我們結(jié)合上面的兩個類將字符串轉(zhuǎn)換大小寫

public class demo8 {
    public static void main(String args[]) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String str = "chenjiabing";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());  //實例化輸入流
        int temp = 0;
        while ((temp = inputStream.read()) != -1)   //讀取緩沖區(qū)的字節(jié)數(shù)據(jù)
        {
            char c = (char) temp;  //將整數(shù)轉(zhuǎn)換成字符,ascii碼的轉(zhuǎn)換
            outputStream.write(Character.toUpperCase(c));  //轉(zhuǎn)換成大寫,然后寫入輸出流的緩沖區(qū)中
        }
        System.out.println(outputStream.toString());   //利用輸出流輸出轉(zhuǎn)換后的字符串,即是去取出內(nèi)存中的數(shù)據(jù)


    }

}

參考文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容