How to concatenate byte array in java?

Java 8Object Oriented ProgrammingProgramming

You ByteArrayOutputStream to write byte arrays and get the result using its toByteArray() method.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Tester {
   public static void main(String[] args) throws IOException {
      byte[] a = { 1,2,3};
      byte[] b = { 4,5,6};
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      baos.write(a);
      baos.write(b);
      byte[] c = baos.toByteArray();
      for(int i=0; i< c.length ; i++){
         System.out.print(c[i] +" ");
      }
   }
}

Output

1 2 3 4 5 6
raja
Published on 06-Feb-2018 16:12:58
Advertisements