Java BufferedOutputStream write()方法

java.io.BufferedOutputStream.write(int) 将指定的字节写入缓冲的输出流。

1 语法

public void write(int b)

2 参数

b:要写入输出流的字节。

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 *  java.io.BufferedOutputStream.Write(int)方法的例子
 */
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bos = null;

        try {
            // create new ByteArrayOutputStream
            baos = new ByteArrayOutputStream();

            // create new BufferedOutputStream with baos
            bos = new BufferedOutputStream(baos);

            // assign integer
            int b = 87;

            // write to stream
            bos.write(b);

            // force the byte to be written to baos
            bos.flush();

            // convert ByteArrayOutputStream to bytes
            byte[] bytes = baos.toByteArray();

            // prints the byte
            System.out.println(bytes[0]);

        } catch(IOException e) {
            // if I/O error occurs.
            e.printStackTrace();
        } finally {
            // releases any system resources associated with the stream
            if(baos!=null)
                baos.close();
            if(bos!=null)
                bos.close();
        }
    }
}

输出结果为:

87

热门文章

优秀文章