日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android BufferedOutputStream的使用

發(fā)布時(shí)間:2024/4/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android BufferedOutputStream的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.



今天,簡單講講android里的BufferedOutputStream的使用。


??BufferedInputStream是帶緩沖區(qū)的輸入流,默認(rèn)緩沖區(qū)大小是8M,能夠減少訪問磁盤的次數(shù),提高文件讀取性能;BufferedOutputStream是帶緩沖區(qū)的輸出流,能夠提高文件的寫入效率。BufferedInputStream與BufferedOutputStream分別是FilterInputStream類和FilterOutputStream類的子類,實(shí)現(xiàn)了裝飾設(shè)計(jì)模式。



構(gòu)造方法

//創(chuàng)建一個(gè)新的緩沖輸出流,以將數(shù)據(jù)寫入指定的底層輸出流。 public BufferedOutputStream(OutputStream out);//創(chuàng)建一個(gè)新的緩沖輸出流,以將具有指定緩沖區(qū)大小的數(shù)據(jù)寫入指定的底層輸出流。 public BufferedOutputStream(OutputStream out,int size);


常用的方法

//向輸出流中輸出一個(gè)字節(jié) public void write(int b);//將指定 byte 數(shù)組中從偏移量 off 開始的 len 個(gè)字節(jié)寫入此緩沖的輸出流。 public void write(byte[] b,int off,int len);//刷新此緩沖的輸出流。這迫使所有緩沖的輸出字節(jié)被寫出到底層輸出流中。 public void flush();


示例代碼:

public class BufferedOutputStreamTest {private static final int LEN = 5;// 對應(yīng)英文字母“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測試函數(shù)*/private static void testBufferedOutputStream() {// 創(chuàng)建“文件輸出流”對應(yīng)的BufferedOutputStream// 它對應(yīng)緩沖區(qū)的大小是16,即緩沖區(qū)的數(shù)據(jù)>=16時(shí),會自動(dòng)將緩沖區(qū)的內(nèi)容寫入到輸出流。try {File file = new File("out.txt");OutputStream out =new BufferedOutputStream(new FileOutputStream(file), 16);// 將ArrayLetters數(shù)組的前10個(gè)字節(jié)寫入到輸出流中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();}} }


運(yùn)行結(jié)果是文件里有20個(gè)字符:abcdefghijklmnopqrst,由于這邊設(shè)置的緩沖區(qū)大小是16,當(dāng)輸入的是20個(gè)字符時(shí)超過了16,不再使用緩沖區(qū),直接將數(shù)據(jù)寫入


基于JDK8的BufferedOutputStream類的源碼:

public class BufferedOutputStream extends FilterOutputStream {/*** The internal buffer where data is stored.*///字符數(shù)組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.*///字符數(shù)組中有效的字節(jié)protected int count;/*** Creates a new buffered output stream to write data to the* specified underlying output stream.** @param out the underlying output stream.*///構(gòu)造函數(shù),字節(jié)數(shù)組大小是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 *///讓緩沖數(shù)據(jù)進(jìn)行寫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.*///寫一個(gè)字節(jié)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個(gè)字節(jié)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. *///當(dāng)輸入的長度大于緩沖區(qū)的長度時(shí),直接寫,不在緩沖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*///將緩沖數(shù)據(jù)寫完public synchronized void flush() throws IOException {flushBuffer();out.flush();} }


說明
BufferedOutputStream的源碼非常簡單,這里就BufferedOutputStream的思想進(jìn)行簡單說明:BufferedOutputStream通過字節(jié)數(shù)組來緩沖數(shù)據(jù),當(dāng)緩沖區(qū)滿或者用戶調(diào)用flush()函數(shù)時(shí),它就會將緩沖區(qū)的數(shù)據(jù)寫入到輸出流中。


從源碼可以看出,BufferedOutputStream的默認(rèn)構(gòu)造函數(shù),緩沖區(qū)字節(jié)數(shù)組大小是8*1024,即8M.


這里簡單講講public void write(byte[] b,int off,int len);這個(gè)函數(shù),這個(gè)函數(shù)可以將b數(shù)組的從off開始的len個(gè)字節(jié)寫入到文件,所以當(dāng)寫入的數(shù)據(jù)大小在變化時(shí),可以新建一個(gè)比較大的數(shù)組,然后通過這個(gè)函數(shù)不停寫入數(shù)據(jù),避免不停的創(chuàng)建不同大小的數(shù)組。


android BufferedOutputStream的使用就講完了。


就這么簡單。



總結(jié)

以上是生活随笔為你收集整理的android BufferedOutputStream的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。