I'm learning how to serialize in Java
my problem I'm having is the code works but I get warnings, I think it's the way I cast it. I'm worried it will give me a problem as I progress.
Unchecked cast from Object to ArrayList warning.
I'm trying to create a account registrations for my simple project.
public class BankAccount implements Serializable {
public String username;
public String password;
public BankAccount() {
super();
// TODO Auto-generated constructor stub
}
public BankAccount(String username, String password) {
super();
this.username = username;
this.password = password;
}
}
Serialization
BankAccount acc1 = new BankAccount("myuser", "mypass");
BankAccount acc2 = new BankAccount("abcd" , "1234");
BankAccount acc3 = new BankAccount("useracbc" , "pw1234");
ArrayList<BankAccount> accounts = new ArrayList<>();
accounts.add(acc1);
accounts.add(acc2);
accounts.add(acc3);
try {
FileOutputStream fileOut = new FileOutputStream("account.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(accounts);
out.close();
System.out.println("Account successfully serialized");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Deserialization
ArrayList<BankAccount> myList = null;
try {
FileInputStream fileIn = new FileInputStream("account.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
myList = (ArrayList<BankAccount>) in.readObject();
fileIn.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(BankAccount list: myList) {
System.out.println(list.username);
System.out.println(list.password);
}
I tried this one from howtodoinjava but I still get the same warning from the casting.
ArrayList<Employee> employeesList = null;
try (FileInputStream fis = new FileInputStream("employeeData");
ObjectInputStream ois = new ObjectInputStream(fis);) {
employeesList = (ArrayList) ois.readObject();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
}
//Verify list data
for (Employee employee : employeesList) {
System.out.println(employee);
}
Regarding unchecked warning: One solution could be to simply do the checks first:
But with the generic type erasure, you have to check the ArrayList type and also all elements. So the code with checks could be something like:
That way you only do checked casts. And be aware: I did no error handling. You should handle the case that the read object is not an ArrayList or does not contain Employee elements.
So this could be valid solution to get rid of the warning. But that is not a solution I would recommend!
Instead: Create your own class Employees. That will store the List internally and will also contain all code required to access the Employees. That way you write an instance of that class and you only have to check one instanceOf instead of checking all employees.