Write a Java Program to Convert the InputStream into Byte Array

Write a Java Program to Convert the InputStream into Byte Array

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.InputStream;
import java.util.Arrays;
import java.io.ByteArrayInputStream;


public class Main {

  public static void main(String args[]) {

    try {

      // create an input stream
      byte[] input = {1, 2, 3, 4};
      InputStream stream = new ByteArrayInputStream(input);
      System.out.println("Input Stream: " + stream);

      // convert the input stream to byte array
      byte[] array = stream.readAllBytes();
      System.out.println("Byte Array: " + Arrays.toString(array));

      stream.close();
    }

    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Final output

Leave a Comment