【发布时间】:2015-05-23 13:24:22
【问题描述】:
我有一个对象数组,我想将它们写入一个文本文件。这样我以后可以在数组中读回对象。我该怎么做? 使用序列化。
反序列化不起作用:
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();
}
【问题讨论】:
-
阅读序列化并申请 ..
-
读取文件有问题。写作工作。
-
您肯定可以比“不工作”更具体吗?是否抛出异常?如果是这样,堆栈跟踪是什么?
-
try catch 中抛出异常,我在注释中写了异常,见行后。
-
将数组包裹在另一个对象中
标签: java object serialization file-io deserialization