how to deserialize an array in java

Solutions on MaxInterview for how to deserialize an array in java by the best coders in the world

showing results for - "how to deserialize an array in java"
Cristina
13 Nov 2016
1//Serialize and deserialize an ArrayList..
2public class Serialize implements Serializable{
3  	//Make sure you implement Serializable on all classes interacting with 
4  	//serialize (class T in this case)
5  	//Serialize an array to a file
6  	FileOutputStream fos= new FileOutputStream("filePath.txt");
7    ObjectOutputStream oos= new ObjectOutputStream(fos);
8    oos.writeObject(arrayListToBeWritten);
9    oos.close();
10    fos.close();	//put try catch around both
11  
12  	//Deserialize an array from a file
13	ArrayList<T> list;
14    FileInputStream fis = new FileInputStream("filePath.txt");
15    ObjectInputStream ois = new ObjectInputStream(fis);
16    list = (ArrayList) ois.readObject();	//Casting
17    ois.close();
18    fis.close();
19  
20  	for (T temp: list) {		//printing the array
21            System.out.println(temp);
22        }