web-dev-qa-db-fra.com

Sérialisation - readObject writeObject remplace

Après avoir écrit le code ci-dessous, je dois maintenant, utiliser des méthodes de remplacement readObject () et writeObject () personnalisées dans StudentData pour lire et écrire les variables de l'objet. Sans utiliser les méthodes defaultWriteObject ou defaultReadObject pour ce faire.

Le problème est que je ne comprends pas bien ce qu'on me demande de faire. J'ai lu tilise readObject/writeObject en sérialisation mais je n'arrive pas à comprendre. Quelqu'un peut-il me diriger dans la bonne direction?

Mon code:

import Java.io.*; //importing input-output files

class Student implements Java.io.Serializable {

    String name; // declaration of variables
    String DOB;
    int id;

    Student(String naam, int idno, String dob) // Initialising variables to user
                                                // data
    {
        name = naam;
        id = idno;
        DOB = dob;
    }

    public String toString() {
        return name + "\t" + id + "\t" + DOB + "\t";
    }

}
import Java.io.BufferedReader;
import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileOutputStream;
import Java.io.IOException;
import Java.io.InputStreamReader;
import Java.io.ObjectInputStream;
import Java.io.ObjectOutputStream;

class StudentData                     //main class
{
    public static void main(String args[]) throws IOException                  //exception handling
    {
        System.out.println("Enter the numbers of students:");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());

        Student[]  students = new Student[n];

        //Student[]  S=new Student[n];                      // array of objects declared and defined
        for (int i = 0; i < students.length; i++) {
            System.out.println("Enter the Details of Student no: " + (i + 1));             //reading data form the user
            System.out.println("Name: ");
            String naam = in.readLine();
            System.out.println("ID no: ");
            int idno = Integer.parseInt(in.readLine());
            System.out.println("DOB: ");               
            String dob = (in.readLine());

            students[i] = new Student(naam, idno, dob);                          

            File studentFile = new File("StudentData.txt");
            try {
                FileOutputStream fileOutput = new FileOutputStream(studentFile);
                ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
                objectOutput.writeObject(students);

                students = null;

                FileInputStream fileInput = new FileInputStream(studentFile);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInput);

                students = (Student[]) objectInputStream.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            for (Student student : students) {
                System.out.println(student);
            }
        }
    }
}
35
Quinn

Vous devez le faire comme ceci:

import Java.io.IOException;

class Student implements Java.io.Serializable {

    String name;
    String DOB;
    int id;

    Student(String naam, int idno, String dob) {
        name = naam;
        id = idno;
        DOB = dob;
    }

    private void writeObject(Java.io.ObjectOutputStream stream)
            throws IOException {
        stream.writeObject(name);
        stream.writeInt(id);
        stream.writeObject(DOB);
    }

    private void readObject(Java.io.ObjectInputStream stream)
            throws IOException, ClassNotFoundException {
        name = (String) stream.readObject();
        id = stream.readInt();
        DOB = (String) stream.readObject();
    }

    public String toString() {
        return name + "\t" + id + "\t" + DOB + "\t";
    }

}

Le readObject est appelé juste après la création d'une instance de Student (en contournant le constructeur normal).

63
antiguru