Java ByteArrayOutputStream writeTo()方法

java.io.ByteArrayOutputStream.writeTo(OutputStream out) 用于将字节数组输出流的完整内容写入指定的输出流。

1 语法

public void writeTo(OutputStream out)

2 参数

out:指定写入的输出流

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) throws IOException {
        byte[] bs = {65, 66, 67, 68, 69, 70, 71, 72};
        OutputStream os = null;
        ByteArrayOutputStream baos = null;

        try {
            // create new output stream
            os = new ByteArrayOutputStream();

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

            // write buffer to the byte array output stream
            baos.write(bs);

            // write to the output stream
            baos.writeTo(os);

            // print the byte as default character set
            System.out.println(os.toString());

        } catch(Exception e) {
            // if I/O error occurs
            e.printStackTrace();
        } finally {
            if(baos!=null)
                baos.close();
            if(os!=null)
                os.close();
        }
    }
}

输出结果为:

ABCDEFGH

热门文章

优秀文章