Java IO Tutorial - Java ByteArrayOutputStream .write (byte[] b, int off, int len)








Syntax

ByteArrayOutputStream.write(byte[] b, int off, int len) has the following syntax.

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

Example

In the following code shows how to use ByteArrayOutputStream.write(byte[] b, int off, int len) method.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
//w ww .  j a va 2 s . c o m
public class Main {
  public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69 };
    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);
    }

  }
}

The code above generates the following result.