Java.io.DataOutputStream.write() 方法
描述
java.io.BufferedInputStream.write(byte[] b, int off, int len) 方法从位置 off 开始的指定字节数组 b 中写入 len 个字节到底层输出流。
声明
以下是 java.io.DataOutputStream.write(byte[] b, int off, int len) 方法的声明 −
public void write(byte[] b, int off, int len)
参数
b − 源缓冲区。
off − 起始位置关闭。
len − 要写入流的字节数。
返回值
此方法不返回任何值。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.DataOutputStream.write(byte[] b, int off, int len) 方法的使用。
package com.tutorialspoint;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
byte[] buf = {87,64,72,31,90};
try {
// create byte array output stream
baos = new ByteArrayOutputStream();
// create data output stream
dos = new DataOutputStream(baos);
// write to the stream from the source buffer
dos.write(buf, 2, 3);
// flushes bytes to underlying output stream
dos.flush();
// for each byte in the baos buffer content
for(byte b:baos.toByteArray()) {
System.out.println(b);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
} finally {
// releases all system resources from the streams
if(dos!=null)
dos.close();
if(baos!=null)
baos.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
72 31 90

