We looked at Serializing a Single Object previously, but what if we need to serialize multiple objects, like an array?
Serializing an Array
Well, we can do that fairly easily as well. Java can take an array of objects, and serialize it. However, it will only work if you de-serialize it as an array.
Consider the write data method.
public static void writeData() {
BankInfo bi_array[] = new BankInfo[10];
bi_array[0] = new BankInfo("Pascale Dillon","1669202783","45477295","9709 Sit Avenue","Dublin","68567","IL");
bi_array[1] = new BankInfo("Nyssa Mcclure","1024221194","71976659","4805 Egestas Av.","Harlow","7384","NE");
bi_array[1] = new BankInfo("John Smith", "12348765", "11122333", "1313 Mockingbird Ln", "", "Glendale", "IN",
"12345");
bi_array[2] = new BankInfo("Zephania Lyons","1838499914","95736738","Ap #731-5841 Eu Avenue","Hà Tĩnh","04156","OK");
bi_array[3] = new BankInfo("Mohammad O'neill","1633389244","83975829","Ap #854-7810 Volutpat Rd.","Ciudad Valles","24756","MT");
bi_array[4] = new BankInfo("Vincent Calhoun","1065978284","66937250","Ap #603-4743 Orci. St.","Bremerhaven","91321","VA");
bi_array[5] = new BankInfo("Germaine Gallagher","953203905","95534969","9635 Aliquam St.","Amiens","669425","GA");
bi_array[6] = new BankInfo("Leonard Oneal","863414344","85178441","Ap #293 Nunc Av.","Castelló","22235","OH");
bi_array[7] = new BankInfo("Hashim Fox","324644414","69404525","775-2805 Fusce Rd.","Mexico City","57481","HI");
bi_array[8] = new BankInfo("Gage Middleton","1267235706","66606676","8052 Adipiscing St.","Campbellton","818466","ID");
bi_array[9] = new BankInfo("Akeem Rice","1991958039","31789136","3628 Lectus Avenue","Redlands","71170","MO");
FileOutputStream fileOut = null;
ObjectOutputStream out = null;
try {
fileOut = new FileOutputStream("bankInfoArray.ser");
out = new ObjectOutputStream(fileOut);
out.writeObject(bi_array);
out.close();
fileOut.close();
// System.out.println(bi.getPrivateInfo());
System.out.println("Serialized data is saved in bankInfo.ser");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
Notice how we have an array of 10 data elements. When we serialize the data, we just pass the whole array. However, if we use it with the read data method used before, it will generate an error, because the read data is expecting a single object, not an array of objects, so we will need to update the read data method.
public static void readData() {
BankInfo biArray[] = null;
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream("bankInfoArray.ser") );
biArray = (BankInfo[]) in.readObject();
for(int i = 0; i < biArray.length; i++) {
System.out.println(biArray[i].getPrivateInfo());
}
} catch (IOException ioe) {
ioe.printStackTrace();
return;
} catch (ClassNotFoundException cnfe) {
System.out.println("BankInfo class not found");
cnfe.printStackTrace();
return;
} catch( Exception e) {
System.out.println("General Exception:" + e.getMessage());
e.printStackTrace();
} finally {
try {
in.close();
} catch(IOException ioe) {}
}
}
Notice that when we typecast the readObject()
result, we cast is as an array of BankInfo
, not just BankInfo
.
Serializing an Array, by Elements
We of course, can also walk through our array, serializing individual elements. This might be helpful if I have a data type other than an array so I’m not limited to a few specific built in data collection types.
In this follow snipet, assume that everything prior is still correct for writing the data. All we’ve done is move into a for loop to write each element individually.
try {
fileOut = new FileOutputStream("bankInfoArray.ser");
out = new ObjectOutputStream(fileOut);
for(int i=0; i < bi_array.length; i++) {
out.writeObject(bi_array[i]);
System.out.println(bi_array[i].getPrivateInfo());
}
System.out.println("Serialized data is saved in bankInfo.ser");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
out.close();
fileOut.close();
} catch(IOException ioe) {}
}
Of course, we cannot read the elements in as an array, because we serialized numerous elements individually, not as an array. Therefore, we will need to read them in individually. The downside to this is if we don’t know the number of elements, we might not create an array of the right size. Therefore we would need to look at how to either add the array size first, or add the objects to a different type of collection (linked list, binary tree, etc – you’ll learn more about those in Intro to Data Structures).
public static void readData() {
BankInfo bi = null;
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream("bankInfoArray.ser") );
while((bi = (BankInfo)in.readObject()) != null) {
System.out.println(bi.getPrivateInfo());
}
} catch (IOException ioe) {
ioe.printStackTrace();
return;
} catch (ClassNotFoundException cnfe) {
System.out.println("BankInfo class not found");
cnfe.printStackTrace();
return;
} catch( Exception e) {
System.out.println("General Exception:" + e.getMessage());
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) { }
}
}
This will give you an error when you read the end of the file, but a proper try/catch will reduce the problems of this.
Serializing Multiple Objects was originally found on Access 2 Learn