Ver Mensaje Individual
  #11 (permalink)  
Antiguo 03/11/2014, 11:04
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: Quisiera leer todos los objetos guardados en un archivo .bin

Buenas,

Yo te propongo quitar ese while(true) y esos try/catch y controlar correctamente cuando hay objetos en el stream.


Código Java:
Ver original
  1. public static void main(String[] args) throws IOException, ClassNotFoundException {
  2.         // TODO Auto-generated method stub
  3.  
  4.         menu();
  5.  
  6.         FileInputStream fis = new FileInputStream("fichero.bin");
  7.  
  8.         try {
  9.             vehiculo vehiculo;
  10.             while (fis.available() > 0) {
  11.                 ObjectInputStream in = new ObjectInputStream(fis);
  12.                 vehiculo = (vehiculo) in.readObject();
  13.                 System.out.println(vehiculo.getMarca());
  14.             }
  15.         } finally {
  16.             fis.close();
  17.         }
  18.     }

Creo que asi es mucho mas simple y provoca menos errores

Un saludo
__________________
If to err is human, then programmers are the most human of us