今天,简单讲讲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 0 through buf.length; elements     * buf[0] through buf[count-1] 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*1024    public 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 len bytes from the specified byte array     * starting at offset off to this buffered output stream.     *     * 

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 * BufferedOutputStreams 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的使用就讲完了。


就这么简单。

更多相关文章

  1. Android下Excel的操作
  2. Android中OpenMax的适配层
  3. 重定向android log
  4. Android(安卓)编程下通过 zipalign 对 APK 文件进行优化
  5. android读取工程里文件并显示在界面
  6. Android(安卓)网络操作(上传下载等)
  7. Android(安卓)读取本地txt文件和写入txt文件到本地
  8. android 操作文件
  9. android camera的基本使用

随机推荐

  1. android adb 通过adb连接制定设备
  2. Android上调用google map api v2
  3. android布局参照示例_约束布局Android示
  4. Android Studio:Fetching android sdk co
  5. android aidl
  6. Android [error cannot connect to daemo
  7. (亲测可用!!)android studio 编译 'com.andro
  8. Android中进行HTTP操作
  9. TimerTask抛不出运行时异常
  10. Android 获取网络状态及调用网络配置界面