For Serializing Array we can use ObjectOutputStream/ObjectInputStream classes.
NOTE: An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
public class SerializeArray {
public static void main(String[] args) {
String[][] twoD = new String[][]{new String[]{"Alex", "Hyde"},
new String[]{"Rohan", "Disuza"}};
String[][] newTwoD = null; // will deserialize to this
System.out.println("Before serialization");
for (String[] arr : twoD) {
for (String val : arr) {
System.out.println(val);
}
}
try {
FileOutputStream fos = new FileOutputStream("test.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(twoD);
FileInputStream fis = new FileInputStream("test.dat");
ObjectInputStream iis = new ObjectInputStream(fis);
newTwoD = (String[][]) iis.readObject();
} catch (Exception e) {
}
System.out.println("After serialization");
for (String[] arr : newTwoD) {
for (String val : arr) {
System.out.println(val);
}
}
}
}
No comments:
Post a Comment