Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/01/2016, 09:45
jcastro3
 
Fecha de Ingreso: marzo-2013
Mensajes: 51
Antigüedad: 11 años, 1 mes
Puntos: 2
Leer todos los objetos guardados en un fichero

Buenas. Estoy intentado leer todos los objetos que tengo en un fichero, el problema es que cuando voy a leer, solo me lee el primer objeto. Dejo los métodos que utilizo para la escritura y la lectura.

Escritura
Código Java:
Ver original
  1. public static void guardarCamiones(...) throws IOException {
  2.         Camiones camion = new Camiones(...);
  3.         camiones.add(camion); //camiones es un arraylist (no puedo prescindir de el)
  4.         File fichero = new File("camiones.obj");
  5.         FileOutputStream fos = new FileOutputStream(fichero, true);
  6.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  7.         for (int i = 0; i < camiones.size(); i++) {
  8.             Camiones cam = camiones.get(i);
  9.             oos.writeObject(cam);
  10.         }
  11.         oos.close();
  12.  
  13.         System.out.println("Se ha escrito en el fichero");
  14.  
  15.     }

Lectura
Código Java:
Ver original
  1. public static void verCamiones() throws ClassNotFoundException, IOException {
  2.         ObjectInputStream ois = null;
  3.         try {
  4.             File fichero = new File("camiones.obj");
  5.             FileInputStream fis = new FileInputStream(fichero);
  6.             ois = new ObjectInputStream(fis);
  7.             while (true) {
  8.                 Camiones camion = (Camiones) ois.readObject();
  9.                 //Mostrar cosas por pantalla
  10.             }
  11.         } catch (IOException io) {
  12.             //io.printStackTrace();
  13.             System.out.println(" ");
  14.         } finally {
  15.             ois.close();
  16.         }
  17.  
  18.     }

Encontré este hilo: http://www.forosdelweb.com/f45/quisi...o-bin-1112682/ pero no me ha dado resultado.

Ayuda por favor, y gracias.