Java ByteArrayOutputStream write String and byte array

Description

Java ByteArrayOutputStream write String and byte array

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) throws IOException {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();

    String string1 = "demo2s.com";
    byte[] bytes1 = new byte[] { 71, 72, 73, 74, 75 };

    stream.write(string1.getBytes());/*from   w w w  . j ava  2 s.c o m*/
    stream.write(bytes1);

    byte[] byteArray = stream.toByteArray();
    System.out.println(Arrays.toString(byteArray));
    System.out.println(new String(byteArray));

  }

}



PreviousNext

Related