我有一個對象數組。我想將該對象寫入文本文件,然後將對象讀回到數組中。我應該怎麼做? 使用序列化。如何在Java中讀取和寫入對象到文本文件?
反序列化不工作:
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();
}
閱讀關於序列化和應用.. – Prashant
在閱讀文件時出現問題。寫作工作。 – Sam
當然你可以比「不工作」更具體嗎?拋出異常嗎?如果是這樣,堆棧跟蹤是什麼? – meriton