目录

void write(byte[] b, int off, int len)

描述 (Description)

java.io.ByteArrayOutputStream.write(byte[] b, int off, int len)方法使用指定的charsetName转换流的内容。 malformed-input和unmappable-characters序列将替换为平台默认字符集的默认替换字符串。

声明 (Declaration)

以下是java.io.ByteArrayOutputStream.write(byte[] b, int off, int len)方法的声明 -

public void write(byte[] b, int off, int len)

参数 (Parameters)

  • b - 指定的缓冲区。

  • off - 在数据中启动偏移。

  • len - 要写入的字节长度。

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.io.ByteArrayOutputStream.write(byte [] b,int off,int len)方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018