Java IO學習筆記四
- 2019 年 12 月 31 日
- 筆記
文章目錄
內存操作流
之前的所有的流操作都是針對文件的,但是有時候只是想要實現數據間轉換,此時如果我們想要創建一個文件然後再刪除文件,那樣顯得有點麻煩,因此此時的內存操作流就顯得很適合這類的操作,因為它只是在內存中存儲,並不會真正的創建文件,內存操作流涉及的兩個類是
ByteArrayInputStream
,ByteArrayOutputStream
.
ByteArrayInputStream
ByteArrayInputStream
包含一個內部緩衝區,該緩衝區包含從流中讀取的位元組。內部計數器跟蹤read
方法要提供的下一個位元組。- 關閉
ByteArrayInputStream
無效。此類中的方法在關閉此流後仍可被調用,而不會產生任何IOException
。 - 主要的功能是從緩衝區讀取位元組
構造函數
ByteArrayInputStream(byte[] buf)
創建一個ByteArrayInputStream
,使用buf
作為其緩衝區數組。ByteArrayInputStream(byte[] buf, int offset, int length)
創建ByteArrayInputStream
,使用 buf 作為其緩衝區數組。
常用的方法
close()
不過對這個無效,因為關閉之後仍然可以使用函數讀取而不報錯int read()
從緩衝區中讀取一個位元組int read(byte[] bytes)
將緩衝區中的內容讀取到數組中int read(byte[] bytes,int off,int len)
將最多len
個數據位元組從此輸入流讀入byte
數組。long skip(long n)
從此輸入流中跳過n
個輸入位元組。void reset()
將此 byte 數組輸出流的 count 字段重置為零,從而丟棄輸出流中目前已累積的所有輸出(清除緩衝區)
實例
public class demo8 { public static void main(String args[]) { String str = "chenjiabingn陳加兵"; byte[] bytes = str.getBytes(); //創建一個數組 ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); //使用bytes作為緩衝區數組 int temp = 0; /*第一種方法讀取緩衝區中的數據,這個和文件的操作不一樣,這個可以直接沖緩衝區中讀取數據位元組*/ while ((temp = inputStream.read()) != -1) { System.out.print((char) temp); } /*創建數組用於存儲讀取的內容,下面是第二種讀取數據的方法*/ byte[] b = new byte[bytes.length]; try { int len = inputStream.read(b); } catch (IOException e) { e.printStackTrace(); } System.out.println(new String(b)); } }
ByteArrayOutputStream
- 此類實現了一個輸出流,其中的數據被寫入一個
byte
數組。緩衝區會隨着數據的不斷寫入而自動增長。可使用toByteArray()
和 toString() 獲取數據。 - 關閉
ByteArrayOutputStream
無效。此類中的方法在關閉此流後仍可被調用,而不會產生任何IOException
。
構造函數
ByteArrayOutputStream()
創建一個新的byte
數組輸出流。ByteArrayOutputStream(int size)
創建一個新的byte
數組輸出流,它具有指定大小的緩衝區容量(以位元組為單位)。
常用函數
int size()
返回緩衝區的當前大小。byte[] toByteArray()
創建一個新分配的byte
數組。String toString()
將緩衝區的位元組轉換成字符串void write(byte[] b, int off, int len)
將指定byte
數組中從偏移量off
開始的len
個位元組寫入此byte
數組輸出流。void write(int b)
將指定的位元組寫入此byte
數組輸出流。
實例
public class demo8 { public static void main(String args[]) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String str = "chenjiabing"; try { outputStream.write(str.getBytes()); //將字符串轉換成數組然後寫入緩衝區 } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.close(); //這裡的關閉無效 } catch (IOException e) { e.printStackTrace(); } } //將緩衝區的數據轉換成字符串後輸出,這裡同樣可以看出輸出流的關閉根本不影響函數的調用 System.out.println(outputStream.size()); //輸出緩衝區的大小 System.out.println(outputStream.toString()); //輸出chenjiabing outputStream.reset(); //清除緩衝區的內容,如果不清零那麼原先寫入的數據還是存在的,但是此時我們已經不需要前面的數據了 try { outputStream.write("陳加兵".getBytes()); //繼續向緩衝區寫入數據 } catch (IOException e) { e.printStackTrace(); } System.out.println(outputStream.size()); //這裡的一個漢字佔了三個位元組 System.out.println(outputStream.toString());//輸出陳加兵 } }
綜合
下面我們結合上面的兩個類將字符串轉換大小寫
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) //讀取緩衝區的位元組數據 { char c = (char) temp; //將整數轉換成字符,ascii碼的轉換 outputStream.write(Character.toUpperCase(c)); //轉換成大寫,然後寫入輸出流的緩衝區中 } System.out.println(outputStream.toString()); //利用輸出流輸出轉換後的字符串,即是去取出內存中的數據 } }
參考文章
- http://blog.csdn.net/yyyandroid/article/details/7756390
- http://www.cnblogs.com/lich/archive/2011/12/11/2283883.html