android BufferedOutputStream的使用
今天,簡單講講android里的BufferedOutputStream的使用。
??BufferedInputStream是帶緩沖區的輸入流,默認緩沖區大小是8M,能夠減少訪問磁盤的次數,提高文件讀取性能;BufferedOutputStream是帶緩沖區的輸出流,能夠提高文件的寫入效率。BufferedInputStream與BufferedOutputStream分別是FilterInputStream類和FilterOutputStream類的子類,實現了裝飾設計模式。
構造方法
//創建一個新的緩沖輸出流,以將數據寫入指定的底層輸出流。 public BufferedOutputStream(OutputStream out);//創建一個新的緩沖輸出流,以將具有指定緩沖區大小的數據寫入指定的底層輸出流。 public BufferedOutputStream(OutputStream out,int size);
常用的方法
//向輸出流中輸出一個字節 public void write(int b);//將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此緩沖的輸出流。 public void write(byte[] b,int off,int len);//刷新此緩沖的輸出流。這迫使所有緩沖的輸出字節被寫出到底層輸出流中。 public void flush();
示例代碼:
public class BufferedOutputStreamTest {private static final int LEN = 5;// 對應英文字母“abcddefghijklmnopqrsttuvwxyz”private static final byte[] ArrayLetters = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A};public static void main(String[] args) {testBufferedOutputStream() ;}/*** BufferedOutputStream的API測試函數*/private static void testBufferedOutputStream() {// 創建“文件輸出流”對應的BufferedOutputStream// 它對應緩沖區的大小是16,即緩沖區的數據>=16時,會自動將緩沖區的內容寫入到輸出流。try {File file = new File("out.txt");OutputStream out =new BufferedOutputStream(new FileOutputStream(file), 16);// 將ArrayLetters數組的前10個字節寫入到輸出流中out.write(ArrayLetters, 0, 20);// 將“換行符\n”寫入到輸出流中out.write('\n');// TODO!out.flush();out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} }
運行結果是文件里有20個字符:abcdefghijklmnopqrst,由于這邊設置的緩沖區大小是16,當輸入的是20個字符時超過了16,不再使用緩沖區,直接將數據寫入
基于JDK8的BufferedOutputStream類的源碼:
public class BufferedOutputStream extends FilterOutputStream {/*** The internal buffer where data is stored.*///字符數組protected byte buf[];/*** The number of valid bytes in the buffer. This value is always* in the range <tt>0</tt> through <tt>buf.length</tt>; elements* <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid* byte data.*///字符數組中有效的字節protected int count;/*** Creates a new buffered output stream to write data to the* specified underlying output stream.** @param out the underlying output stream.*///構造函數,字節數組大小是8*1024public BufferedOutputStream(OutputStream out) {this(out, 8192);}/*** Creates a new buffered output stream to write data to the* specified underlying output stream with the specified buffer* size.** @param out the underlying output stream.* @param size the buffer size.* @exception IllegalArgumentException if size <= 0.*/public BufferedOutputStream(OutputStream out, int size) {super(out);if (size <= 0) {throw new IllegalArgumentException("Buffer size <= 0");}buf = new byte[size];}/** Flush the internal buffer *///讓緩沖數據進行寫private void flushBuffer() throws IOException {if (count > 0) {out.write(buf, 0, count);count = 0;}}/*** Writes the specified byte to this buffered output stream.** @param b the byte to be written.* @exception IOException if an I/O error occurs.*///寫一個字節public synchronized void write(int b) throws IOException {if (count >= buf.length) {flushBuffer();}buf[count++] = (byte)b;}/*** Writes <code>len</code> bytes from the specified byte array* starting at offset <code>off</code> to this buffered output stream.** <p> Ordinarily this method stores bytes from the given array into this* stream's buffer, flushing the buffer to the underlying output stream as* needed. If the requested length is at least as large as this stream's* buffer, however, then this method will flush the buffer and write the* bytes directly to the underlying output stream. Thus redundant* <code>BufferedOutputStream</code>s will not copy data unnecessarily.** @param b the data.* @param off the start offset in the data.* @param len the number of bytes to write.* @exception IOException if an I/O error occurs.*///從b中off位置開始寫len個字節public synchronized void write(byte b[], int off, int len) throws IOException {if (len >= buf.length) {/* If the request length exceeds the size of the output buffer,flush the output buffer and then write the data directly.In this way buffered streams will cascade harmlessly. *///當輸入的長度大于緩沖區的長度時,直接寫,不在緩沖flushBuffer();out.write(b, off, len);return;}if (len > buf.length - count) {flushBuffer();}System.arraycopy(b, off, buf, count, len);count += len;}/*** Flushes this buffered output stream. This forces any buffered* output bytes to be written out to the underlying output stream.** @exception IOException if an I/O error occurs.* @see java.io.FilterOutputStream#out*///將緩沖數據寫完public synchronized void flush() throws IOException {flushBuffer();out.flush();} }
說明:
BufferedOutputStream的源碼非常簡單,這里就BufferedOutputStream的思想進行簡單說明:BufferedOutputStream通過字節數組來緩沖數據,當緩沖區滿或者用戶調用flush()函數時,它就會將緩沖區的數據寫入到輸出流中。
從源碼可以看出,BufferedOutputStream的默認構造函數,緩沖區字節數組大小是8*1024,即8M.
這里簡單講講public void write(byte[] b,int off,int len);這個函數,這個函數可以將b數組的從off開始的len個字節寫入到文件,所以當寫入的數據大小在變化時,可以新建一個比較大的數組,然后通過這個函數不停寫入數據,避免不停的創建不同大小的數組。
android BufferedOutputStream的使用就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android BufferedOutputStream的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 从文件制定位置读取数据
- 下一篇: android jni 释放资源