Reputation: 186
I have an array of Objects stored in "rootFile". I would like to read back the objects into another ArrayList. So far I tried this:
List<NoteCard> cardArray = new ArrayList<>();
try {
FileInputStream fis = openFileInput(rootFile);
ObjectInputStream ois = new ObjectInputStream(fis);
cardArray = (List<NoteCard>)ois.readObject();
ois.close();
fis.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
Upvotes: 0
Views: 61
Reputation: 9388
You can remove the unchecked cast by doing this:
ArrayList<NoteCard> cardArray = new ArrayList<>();
try {
FileInputStream fis = openFileInput(rootFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
if (object instanceof ArrayList) {
ArrayList arrayList = (ArrayList) object;
for (Object object : arrayList) {
cardArray.add((NoteCard) object);
}
}
ois.close();
fis.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
Upvotes: 1