2012-10-18 15 views
29

नीचे दिए गए कोड को लिखने के बाद अब मुझे ऑब्जेक्ट के चर पढ़ने और लिखने के लिए StudentData में कस्टम readObject() और writeObject() लिखना ओवरराइड विधियों का उपयोग करना होगा। ऐसा करने के लिए डिफ़ॉल्ट WriteObject या defaultReadObject विधियों का उपयोग किए बिना।सीरियलाइजेशन - readObject writeObject ओवरराइड

समस्या यह है कि मुझे पूरी तरह से समझ में नहीं आता कि मुझे क्या करने के लिए कहा जा रहा है। मैंने Uses of readObject/writeObject in Serialization पढ़ा है लेकिन मुझे इसके चारों ओर अपना सिर नहीं मिल सकता है। क्या कोई मुझे सही दिशा में इंगित कर सकता है?

मेरे कोड:

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); 
      } 
     } 
    } 
} 
+0

अजीब असाइनमेंट। आपको * माना जाता है * डिफ़ॉल्ट WriteObject() और defaultReadObject() को कॉल करने के लिए। – EJP

उत्तर

49

आप इस तरह यह क्या करना है: केवल छात्र का एक उदाहरण बनाने के बाद

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"; 
    } 

} 

readObject शुरू हो जाती है (सामान्य निर्माता दरकिनार)।

+7

बस वहां एक विवरण इंगित करें। आदेश या लेखन/पढ़ना ** ** महत्वपूर्ण है। उदाहरण लिखने में है: नाम, आईडी, डीओबी ताकि – Manu

+3

पढ़ते समय आपको नाम, आईडी, डीओबी मिल जाएगा, क्या यह खाली कन्स्ट्रक्टर के बिना काम करेगा? – borjab