我有这个类Dictionar,实现Serializable和类ScriereCitire 包装控制器;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ScriereCitire {
public void scriere(Dictionar dictionar){
try (FileOutputStream fileOut = new FileOutputStream("dict.txt")){
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(dictionar);
objOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public Dictionar citire(){
try(FileInputStream fileInput = new FileInputStream("dict.txt")){
ObjectInputStream objInput = new ObjectInputStream(fileInput);
@SuppressWarnings("unused")
Dictionar dictionar = (Dictionar)objInput.readObject();
objInput.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
我在项目中创建了一个名为dict.txt的文件。我有一个类缅因州,我称之为我的GraphicInterface,它叫做ScriereCitire。但是当我运行它时指向这个错误:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at controler.ScriereCitire.citire(ScriereCitire.java:27)
at view.GUI.<init>(GUI.java:46)
at controler.Main.main(Main.java:11)
答案 0 :(得分:1)
你以某种方式创建了一个空文件。可能你有一个异常创建它并忽略它。
我在项目中创建了一个名为dict.txt的文件
如何?为什么呢?
NB序列化数据不是文本,不应放在名为.txt的文件中。