Java Problems With Deserialization

I know that this question has been addressed before,but i simply cannot find the answer no matter how hard i try. I am trying to serialize and deserialize objects in Java. I am having problems in the deserialization. I do not get the values that were entered, but something along the lines of [email protected] Why do i get this instead of the actual values typed in?

Here is my code

package prueba;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Prueba {


public static void main(String[] args) throws FileNotFoundException,IOException, ClassNotFoundException {
    File file = new File("f.txt");
    List <Estudiantes> lista = new ArrayList<>();
    boolean continuar = true;
    while (continuar == true){
    Estudiantes es = new Estudiantes();
    System.out.println("Ingrese nombre");
    Scanner kb = new Scanner(System.in);
    es.nombre = kb.nextLine();
    System.out.println("Ingrese Apellido");
    Scanner kb1 = new Scanner(System.in);
    es.apellido = kb1.nextLine();
    System.out.println("Ingrese Número");
    Scanner kb2 = new Scanner(System.in);
    es.numero = kb2.nextInt();
    lista.add(es);

    FileOutputStream fo = new FileOutputStream(file);
    ObjectOutputStream output = new ObjectOutputStream(fo);

    for (Estudiantes est: lista){
        output.writeObject(est);
    }
    output.close();
    fo.close();


    FileInputStream fi = new FileInputStream(file);
    ObjectInputStream input = new ObjectInputStream(fi);
    ArrayList<Estudiantes> est2 = new ArrayList<Estudiantes>();
    try {
        while (true){
            Estudiantes s = (Estudiantes)input.readObject();
            est2.add(s);
        }
    }
    catch (EOFException ex){}
    for (Estudiantes s :est2){
        System.out.println(s);
    fi.close();
    input.close();
        }

    System.out.println("0 para salir; 1 para continuar");
    Scanner kb3 = new Scanner(System.in);
    int rev = kb3.nextInt();

    if (rev == 0){
        continuar = false;
        System.out.println("Hasta Luego");
    } 
    }
   }
 }

And here is my Estudiantes class

package prueba;

import java.io.Serializable;

public class Estudiantes implements Serializable{
    String nombre, apellido;
    int numero;

}

Thanks

Answer

When you try to print your class when reading back the objects you previously wrote you have to implement the toString() method from the Object class here is what I mean. Change your class to this:

public class Estudiantes implements Serializable {
    private static final long serialVersionUID = 123L; // has to be unique
    String nombre, apellido;
    int numero;

    @Override
    public String toString() {
        System.out.println("first name: " + nombre);
        System.out.println("last name:  " + apellido);
        System.out.println(numero);
        return // a string you want to print
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *