Assume serialise.bin is a file that is full of words and was an ArrayList when it was serialised
public static ArrayList<String> deserialise(){
ArrayList<String> words= new ArrayList<String>();
File serial = new File("serialise.bin");
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(serial))){
System.out.println(in.readObject()); //prints out the content
//I want to store the content in to an ArrayList<String>
}catch(Exception e){
e.getMessage();
}
return words;
}
I want to be able to deserialise the "serialise.bin" file and store the content in an ArrayList
Cast it to
ArrayList<String>
, asin.readObject()
does return anObject
, and assign it towords
:The annotation
@SuppressWarnings("unchecked")
can be added to suppress a type-safety warning. It occurs, because you have to cast anObject
to a generic type. With Java's type erasure there is no way of knowing for the compiler, if the cast is type-safe at runtime. Here is another post on this. Moreovere.getMessage();
does nothing, print it or usee.printStackTrace();
instead.