import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

class Record implements Serializable
{

        int roll;
        String name;

        public void setData()
        {
            Scanner scr=new Scanner(System.in);
            System.out.println("Enter Roll Number: ");
            roll=scr.nextInt();
            System.out.println("Enter Name: ");
            name=scr.next();
            writeData();

        }
        public void writeData()
        {
            try {
                ObjectOutputStream of=new ObjectOutputStream(new FileOutputStream("c:\\yyy.txt",true));
                of.flush();
                Object o =new Object();
                o=(Object)this;
                of.writeObject(o);
                //of.reset();
                of.flush();
                of.close();
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

        }
        public  void readData()
        {
            Record r;
            try {
                ObjectInputStream iff=new ObjectInputStream(new FileInputStream("c:\\yyy.txt"));
                while(true)
                {
                    r=(Record)iff.readObject();

                    r.getData();

                }





            }catch(EOFException e)
            {
                System.err.print("hi");
            }
            catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            } catch (ClassNotFoundException e) {

                e.printStackTrace();
            }

        }
        public void getData()
        {
            System.out.println("Roll Number: " +roll);
            System.out.println("Name : "+name);
        }

}
public class java  implements Serializable{


    public static void main(String[] args) {
        Scanner scr=new Scanner(System.in);
        int ch;
        Record obj=new Record(); 
        while(true)
        {
                System.out.println("1::::::::WriteData");
                System.out.println("2::::::::readData");
                System.out.println("3::::::::Exit");
                System.out.println("Enter Choice ");
                ch=scr.nextInt();
                switch(ch)
                {
                case 1:
                        obj.setData();
                        break;
                case 2:
                        try {
                            Object o;
                            ObjectInputStream iff=new ObjectInputStream(new FileInputStream("c:\\yyy.txt"));
                            while(true)
                            {
                                o=iff.readObject();
                                obj=(Record)o;
                            obj.getData();

                        }   
                        }catch(EOFException e)
                        {
                            System.err.print("hi");
                        }
                        catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {   
                        e.printStackTrace();
                        } catch (ClassNotFoundException e) {

                        e.printStackTrace();
                        }
                        break;
                case 3:
                        System.exit(0);
                }
        }
    }

}

Can you tell us what you are having difficulty with, please?

Hello,
the problem was that you were trying to readObject() too many times. So what was happening was something like :

while(true) {
     while (true) {
         readObject();
     }
     readObject();
 }

see what am sayin' ... :)

Here is my vesion of your program :

package test;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;
public class Record implements Serializable
{
    private static final long serialVersionUID = 1;

        private int roll;
        private String name;

        public void setData()
        {
            Scanner scr=new Scanner(System.in);
            System.out.println("Enter Roll Number: ");
            roll=scr.nextInt();
            System.out.println("Enter Name: ");
            name=scr.next();
            writeData();
        }
        public void writeData()
        {
            try {
                ObjectOutputStream of = new ObjectOutputStream(new FileOutputStream("c:\\yyy.txt",true));
                of.writeObject(this);
                of.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public Record readData()
        {
            Record r = new Record();
            try {
                ObjectInputStream iff=new ObjectInputStream(new FileInputStream("c:\\yyy.txt"));
                r =(Record)iff.readObject();
                r.getData();
            }catch(EOFException e)
            {   
                System.err.println(e.getMessage());
                System.err.print("hi");
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return r;
        }
        public void getData()
        {
            System.out.println("Roll Number: " + roll);
            System.out.println("Name : "+ name);
        }



    public static void main(String[] args) {
        Scanner scr = new Scanner(System.in);
        int ch;
        Record obj = new Record(); 
        while(true)
        {
                System.out.println("1::::::::WriteData");
                System.out.println("2::::::::readData");
                System.out.println("3::::::::Exit");
                System.out.println("Enter Choice ");
                ch=scr.nextInt();
                switch(ch)
                {
                case 1:
                        obj.setData();
                        break;
                case 2:
                        Record teRecord = new Record();
                        teRecord.readData();
                        break;
                case 3:
                        System.exit(0);
                }
        }
    }
}

ya , Solved !!
Thank you very much

You're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.