Helpt with reading files

31 views Asked by At

I'm solving an exercise for class. I have a problem with reading binary files. I have made a function to write different objects to a file and it works fine. But when I read it, it only prints one. Can somebody help me?

Main

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    
        int opcion = 0;
        
        
        System.out.println("Nombre del fichero:");
        String nombreFichero = sc.nextLine();
        
        File file = new File(nombreFichero);
        
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FicheroEmpleados fich = new FicheroEmpleados (nombreFichero);
        
        
        do {
            System.out.println("Menú");
            System.out.println("1. Agregar empleado");
            System.out.println("2. Mostrar empleados");
            System.out.println("3. Salir");
            opcion = sc.nextInt();
            
            switch(opcion) {
            case 1:
                
                try {
                System.out.println("Nombre del empleado: ");
                String nombre = sc.next();      
                System.out.println("Edad: ");
                int edad = sc.nextInt();
                System.out.println("Salario: ");
                double salario = sc.nextDouble();
                Empleado e = new Empleado(nombre, edad, salario);
                System.out.println("Datos correctos");
                fich.agregarEmpleado(e);
                } catch (Exception ex) {
                    System.out.println("Datos incorrectos");
                }
                break;
            case 2:
                fich.mostrarEmpleado();
                break;
            case 3:
                break;
            default:
                System.out.println("Opcion incorrecta");
                break;
            }
            
        } while (opcion!=3);
        

    }

Class for control de files

public class FicheroEmpleados {
 
    String nombreFichero;
 
    public FicheroEmpleados (String nombreFichero) {
     this.nombreFichero = nombreFichero;
 }
    
    public void agregarEmpleado(Empleado e) {
        
        try {
            
            FileOutputStream fichero = new FileOutputStream(nombreFichero, true);
            ObjectOutputStream salida = new ObjectOutputStream(fichero);
            
            salida.writeObject(e);
            System.out.println("Empleado guardado");
            salida.close();
            
        } catch (FileNotFoundException ex) {
            System.out.println("No se ha encontrado el fichero");
        } catch (IOException ex) {
            System.out.println("No se ha podido escribir el fichero");
        } /*finally {
            try {
                salida.close();
            } catch (IOException ex) {
                System.out.println("No se ha podido cerrar el fichero");
            }
        }
    */}
    
    public void mostrarEmpleado() {
        try {
            FileInputStream fichero = new FileInputStream(nombreFichero);
            ObjectInputStream entrada = new ObjectInputStream (fichero);
            
            while(true) {
                Empleado e = (Empleado) entrada.readObject();
                
            
                System.out.println("Nombre: " + e.getNombre());
                System.out.println("Edad: " + e.getEdad());
                System.out.println("Salario: " + e.getSalario());
                entrada.close();
            }
        
        //* fichero.close();
        }catch(EOFException ex) {
            return;
        }catch (FileNotFoundException ex) {
            System.out.println("No se encuentra el fichero");
        }catch (IOException ex) {
            System.out.println("No se puede leer el fichero");
        }catch (ClassNotFoundException ex) {
            System.out.println("No se ha podido encontrar la clase");
        } 
    }
    
}

Serialized class

public class Empleado implements Serializable {
    private String nombre;
    private int edad;
    private double salario;
    
    //constructor de Empleado
    public Empleado() {}
    
    public Empleado (String nombre, int edad, double salario) {
        this.nombre = nombre;
        this.edad = edad;
        this.salario = salario;
    }
    
    //getters y setters de Empleado
    public String getNombre() {
        return this.nombre;
    }
    
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    
    public int getEdad() {
        return this.edad;
    }
    
    public void setEdad(int edad) {
        this.edad = edad;
    }
    
    public double getSalario() {
        return this.salario;
    }
    
    public void setSalario(double salario) {
        this.salario = salario;
    }
    public String toString() {
        return "Empleado: "+ nombre + " edad: "+ edad+ " salario: "+ salario;
    }
    

}

Nombre del fichero: prueba3 Menú

  1. Agregar empleado
  2. Mostrar empleados
  3. Salir 1 Nombre del empleado: toni Edad: 33 Salario: 333333 Datos correctos Empleado guardado Menú
  4. Agregar empleado
  5. Mostrar empleados
  6. Salir 2 Nombre: vicent Edad: 22 Salario: 2222.0 No se puede leer el fichero Menú
  7. Agregar empleado
  8. Mostrar empleados
  9. Salir
0

There are 0 answers