How to concatenate byte array in java?


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

mkotla
mkotla

e

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements