Answers for "how to serialize an array in java"

0

how to serialize an array in java

//Serialize and deserialize an ArrayList..
public class Serialize implements Serializable{
  	//Make sure you implement Serializable on all classes interacting with 
  	//serialize (class T in this case)
  	//Serialize an array to a file
  	FileOutputStream fos= new FileOutputStream("filePath.txt");
    ObjectOutputStream oos= new ObjectOutputStream(fos);
    oos.writeObject(arrayListToBeWritten);
    oos.close();
    fos.close();	//put try catch around both
  
  	//Deserialize an array from a file
	ArrayList<T> list;
    FileInputStream fis = new FileInputStream("filePath.txt");
    ObjectInputStream ois = new ObjectInputStream(fis);
    list = (ArrayList) ois.readObject();	//Casting
    ois.close();
    fis.close();
  
  	for (T temp: list) {		//printing the array
            System.out.println(temp);
        }
Posted by: Guest on March-05-2021

Code answers related to "how to serialize an array in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language