Sam*_*Sam 5 java file-io serialization object deserialization
我有一个对象数组,我想将它们写入文本文件中。这样我以后就可以读回数组中的对象。我该怎么做? 使用序列化。
反序列化不起作用:
public static void readdata(){
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream("myfile.txt")); // getting end of file exception here
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
array = (players[]) input.readObject(); // null pointer exception here
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readdata();
writedata();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19898 次 |
最近记录: |