【发布时间】:2023-03-19 08:00:01
【问题描述】:
假设serialise.bin是一个充满单词的文件,序列化时是一个ArrayList
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;
}
我希望能够反序列化“serialise.bin”文件并将内容存储在 ArrayList 中
【问题讨论】:
-
你有什么问题?你的代码有问题吗?
-
不要返回
ArrayList。相反,返回List,这样deserialise的调用者就不会依赖于该实现细节。
标签: java arraylist deserialization fileinputstream objectinputstream