Convert Arraylist to Byte Array Java

In this tutorial we will learn how to convert ArrayList to byte array java . We will write a java program to cconvert arraylist to byte array java using ByteArrayOutputStream and ObjectOutputStream class .

Java Program To Convert ArrayList to Byte Array Java

Let’s discuss approach to convert arraylist to byte array java . We will discuss first our approach then implement java program .

Step-1:- To convert the ArrayList to arrays of byte we need to use  ByteArrayOutputStream and ObjectOutputStream class.

Step-2:  Now we will create an object of the list and a byte variable then instantiate the list object to ArrayList in java.

Step-3: Now we will add the elements to ArrayList one by one by using add() method.

Step-4: Then we will convert the array list to a byte array using ByteArrayOutputStream and ObjectOutputStream class.

Step-5: We will handle exception While converting the ArrayList to byte array using try and catch .

We have used classes ByteArrayOutputStream and ObjectOutputStream which are basically used to implement an out stream to write data into byte array and then read the data. Let’s see Java program to convert arraylist to byte array java .

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 ConvertArraylistToByteArrayInJava {
	public static void main(String[] args) {
		
		List<String> list = new ArrayList<String>();
		//add element in ArrayList 
	      list.add("A");
	      list.add("B");
	      list.add("C");
	      list.add("D");
	      list.add("E");
	      //print arraylist
	      System.out.println("Print 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 is : " + Arrays.toString(bytes));
		
	}

}

Output:

Print given arraylist: [A, B, C, D, E]
ArrayList is successfully converted to Byte Array
Byte array is : [-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, 1, 65, 116,
 0, 1, 66, 116, 0, 1, 67, 116, 0, 1, 68, 116, 0, 1, 69, 120]

In this tutorial we learned How to convert arraylist to byte array java using ByteArrayOutputStream and ObjectOutputStream class . We have written java program which convert arraylist to byte array in java.we create arraylist and convert to byte array . you can see more java program for practice .

Leave a Reply

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

13 + = 19