Java.io.ByteArrayOutputStream.write() 方法
描述
java.io.ByteArrayOutputStream.write(byte[] b, int off, int len) 方法使用指定的 charsetName 转换流的内容。 格式错误的输入和不可映射的字符序列被平台默认字符集的默认替换字符串替换。
声明
以下是 java.io.ByteArrayOutputStream.write(byte[] b, int off, int len) 方法的声明 −
public void write(byte[] b, int off, int len)
参数
b − 指定的缓冲区。
off − 数据中开始的偏移量。
len − 要写入的字节长度。
返回值
此方法不返回任何值。
异常
NA
示例
下面的例子展示了 java.io.ByteArrayOutputStream.write(byte[] b, int off, int len) 方法的使用。
package com.tutorialspoint;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamDemo {
public static void main(String[] args) throws IOException {
byte[] bs = {65, 66, 67, 68, 69};
ByteArrayOutputStream baos = null;
try {
// create new ByteArrayOutputStream
baos = new ByteArrayOutputStream();
// write byte array to the output stream
baos.write(bs, 3, 2);
// read all the bytes in the output stream
for (byte b: baos.toByteArray()) {
System.out.println(b);
}
} catch(Exception e) {
// if I/O error occurs
e.printStackTrace();
} finally {
if(baos!=null)
baos.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
68 69

