public static void leeFichero(Agenda agenda) throws Exception{
FileInputStream ficheroAgenda2;
try{
ficheroAgenda2 = new FileInputStream("agenda.bin");
ObjectInputStream ois = new ObjectInputStream(ficheroAgenda2);
agenda=(Agenda) ois.readObject();
ois.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
La solución es bien sencilla. Simplemente lo que hacemos es devolver la agenda y recogerla en la llamada a la función. De esta forma se solucionaría el problema.
public static Agenda leeFichero() throws Exception{
FileInputStream ficheroAgenda2;
try{
try{
Agenda agenda = new Agenda();
ficheroAgenda2 = new FileInputStream("agenda.bin");
ObjectInputStream ois = new ObjectInputStream(ficheroAgenda2);
agenda=(Agenda) ois.readObject();
ois.close();
ficheroAgenda2 = new FileInputStream("agenda.bin");
ObjectInputStream ois = new ObjectInputStream(ficheroAgenda2);
agenda=(Agenda) ois.readObject();
ois.close();
return agenda; }
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
y en el programa principal recogeríamos el valor de retorno:
agenda=leeFichero();
Si que a veces las cosas más fáciles... se nos escapan!
No hay comentarios:
Publicar un comentario