我遇到以下EOFException:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2903)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1502)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at Practica.Animalario.Poblacion.abrirPoblacion(Poblacion.java:37)
at InterfazGrafica.Ventana.actionPerformed(Ventana.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
执行我编写的代码时。此代码的目的是将对象读/写到文件中:
public class Poblacion implements Serializable{
public Raton poblacion [];
public String nombrePoblacion;
public String responsable;
public int dias;
public Poblacion (){
poblacion = null;
nombrePoblacion = "";
responsable = "";
dias = 0;
}
public Poblacion (String nombre, String cientifico, int _dias){
poblacion = null;
nombrePoblacion = nombre;
responsable = cientifico;
dias = _dias;
}
public class Raton {
String codigoReferencia;
Date fechaDeNacimiento;
int peso;
String sexo;
float temperatura;
String observaciones;
char X1;
char X2;
char Y1;
public void Raton(String _codigo, Date _fecha, int _peso, String _sexo, float _temp, String _obs, char _x1, char _x2){
codigoReferencia = _codigo;
fechaDeNacimiento = _fecha;
peso = _peso;
sexo = _sexo;
if (sexo.equals("Macho")) {
X1=_x1;
Y1=_x2;
} else {
X1=_x1;
X2=_x2;
}
temperatura=_temp;
observaciones=_obs;
}
public String toString(){
return codigoReferencia+fechaDeNacimiento+peso+sexo+temperatura+X1+X2+Y1+observaciones;
}
}
public static Poblacion abrirPoblacion (File file) throws ClassNotFoundException, IOException {
File archivo = file;
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
Poblacion poblacion=new Poblacion();
try {
fileInputStream = new FileInputStream(archivo);
objectInputStream = new ObjectInputStream(fileInputStream);
System.out.println(objectInputStream.readObject());
poblacion = (Poblacion) objectInputStream.readObject();
} catch(IOException ex) {
ex.printStackTrace();
} finally {
try {
if (objectInputStream!=null) {
objectInputStream.close();
}
} catch(IOException ex) {
ex.printStackTrace();
}
try {
if (fileInputStream!=null) {
fileInputStream.close();
}
} catch(IOException ex) {
ex.printStackTrace();
}
return poblacion;
}
}
public static void crearPoblacion (String _nombre,String _responsable, int _dias) {
File archivo = new File("C:\\Users\\rober\\OneDrive - Fundación Universitaria San Pablo CEU\\Java\\Práctica 1"+"\\"+_nombre+".dat");
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
Poblacion nuevaPoblacion = new Poblacion(_nombre,_responsable,_dias);
try {
fileOutputStream = new FileOutputStream(archivo);
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(nuevaPoblacion);
} catch(IOException ex) {
ex.printStackTrace();
} finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch(IOException ex) {
ex.printStackTrace();
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:1)
当文件中只有一个对象时,您正在调用readObject()
两次。
System.out.println(objectInputStream.readObject());
poblacion=(Poblacion) objectInputStream.readObject();
显然,它到达文件的末尾并且不能再读取第二个文件。只需更改此项,以便您不要尝试两次阅读:
poblacion=(Poblacion) objectInputStream.readObject();
System.out.println(poblacion);