Convert ArrayList to Byte Array Java

Convert ArrayList to Byte Array Java | In this section, we will write a Java program to convert a list of arrays to a byte array. The byte is one of the eight primitive data types in java it ranges from -128 to 127, whereas the list is an ordered collection of objects of similar data type it allows duplicate values, insertion, and deletion of elements. The Byte array is used to store byte data types only.

The explanation of the convert ArrayList to byte array Java code is as follows:-

Step-1:- To convert the list of arrays to byte we need to import some classes available in Java. Those are ByteArrayOutputStream, IOException, ObjectOutPutStream, ArrayList, and a List classes.
Step-2: Then in the main class we create an object of the list and a byte variable then instantiate the list object to ArrayList.

Step-3: Then add the elements to ArrayList one by one by using add() method.
Step-4: Later by using ByteArrayOutputStream to convert the array list to a byte array.
Step-5: While converting the ArrayList to byte array it might throw an exception in order to handle this exception we are using try and catch methods.

Convert ArrayList to Byte Array Java Program

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

   public static void main(String[] args) {
      List<String> list = new ArrayList<String>();
      list.add("Array");
      list.add("list");
      list.add("to");
      list.add("byte");
      list.add("array");
      System.out.println("Given ArrayList: " + list);
      
      ByteArrayOutputStream output = 
                  new ByteArrayOutputStream();
      ObjectOutputStream obj;
      try {
         obj = new ObjectOutputStream(output);
         obj.writeObject(list);
      } catch (IOException e) {
         e.printStackTrace();
      }

      byte[] bytes = output.toByteArray();
      System.out.println("ArrayList is successfully "+
                         "converted to Byte Array");
      System.out.println("Byte array: " + Arrays.toString(bytes));
   }
}

Output:-

Given ArrayList: [Array, list, to, byte, array]
ArrayList is successfully converted to Byte Array
Byte array: [-84, -19, 0, 5, 115, 114, 0, 19, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114, 97, 121, 76, 105, 115, 116, 120, -127, -46, 29, -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105, 122, 101, 120, 112, 0, 0, 0, 5, 119, 4, 0, 0, 0, 5, 116, 0, 5, 65, 114, 114, 97, 121, 116, 0, 4, 108, 105, 115, 116, 116, 0, 2, 116, 111, 116, 0, 4, 98, 121, 116, 101, 116, 0, 5, 97, 114, 114, 97, 121, 120]

In the above convert ArrayList to byte array Java program, we have taken an ArrayList of String. Some string elements are added to the ArrayList. After that ArrayList is converted to a byte array using ByteArrayOutputStream, and ObjectOutputStream.

The ByteArrayOutputStream & ObjectOutputStream are the child class of OutputStream class. The writeObject() method of ObjectOutputStream and toByteArray() method of ByteArrayOutputStream class is used to solve the problem.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *