Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:22:50+00:00 2026-06-13T09:22:50+00:00

Having written the code below I now have to,use custom readObject() and writeObject() override

  • 0

Having written the code below I now have to,use custom readObject() and writeObject() override methods in StudentData to read and write the object’s variables. Without using the defaultWriteObject or defaultReadObject methods to do this.

The trouble is that I don’t understand fully what am being asked to do. I have read Uses of readObject/writeObject in Serialization but I can’t get my head around it.Can someone point me in the right direction?

My 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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:22:51+00:00Added an answer on June 13, 2026 at 9:22 am

    You have to do it like this:

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

    The readObject is invoked just after creating an instance of Student (bypassing the normal constructor).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an autogenerating sitemap code below, it is called every 12 hours.
I have written a simple upload component in Flash, but I am now having
I am having this problem that when I run the below written code in
I'm often having code written as follows try: self.title = item.title().content.string except AttributeError, e:
I'm having a problem with some code I've written. I've had to anonymize it,
I have written a SQL query that works just fine, but am having a
I am having difficulties referencing all 3. I have already written my XML,XSD and
I am having a real headache. I have written a small and simple menu
I have written a piece of code and it runs fine on my system.
I have written a bunch of entities. I am trying to test them now.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.