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();
}
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.