Hey guys, check this out:
File test = new File(path + "person.data");
FileOutputStream fos = new FileOutputStream(test);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p = new Person();
Person p2 = new Person();
p.setName("Sub Zero");
p.setGender("Male");
p2.setName("Sonya Blade");
p2.setGender("Female");
ArrayList<Person> people = new ArrayList<Person>();
ArrayList<Person> people2 = new ArrayList<Person>();
people.add(p);
people.add(p2);
oos.writeObject(people);
System.out.println("People array has been written");
oos.close();
FileInputStream fis = new FileInputStream(test);
ObjectInputStream ois = new ObjectInputStream(fis);
people2 = (ArrayList<Person>) ois.readObject();
Person m = people2.get(1);
System.out.println("From file: " + m.getName() + ", " + m.getGender());
fis.close();
This works. However, my IDE puts a yellow line underneath the code "people2 = (ArrayList<Person>) ois.readObject()", giving me the warning: unchecked cast from object to arraylist<Person>.
Should I ignore the warning, or is there a better way to code this?