1

i saved object of class students in file named file.txt i don't know how many student objects are in the file. how can i Use EOFException to end the loop to. and i cant correct that exception can any help

students class
 class students implements java.io.Serializable {
String Frist;
String Second;
String Phone;
String CityAddress;
String CityStreet;
students(){}
students(String s1,String s2,String s3,String s4,String s5){
    Frist=s1;
    Second=s2;
    Phone=s3;
    CityAddress=s4;
    CityStreet=s5;

}

}
this i try in main but give exception
public static void main(String[] args) throws Exception {
    // TODO code application logic here

        students  s;
        ObjectInputStream input =new ObjectInputStream(new FileInputStream("file.txt"));
        while(true)
        {
            try
            {
                s=(students)(input.readObject());
                System.out.print(s.Frist +" ");
                System.out.print(s.Second+" ");
                System.out.print(s.Phone+" ");
                System.out.print(s.CityAddress+" ");
                System.out.println(s.CityStreet);
            }catch(EOFException ex)
            {
                break;
            }

        }

        input.close();


}

Here's the exception stacktrace:

Exception in thread "main" java.io.InvalidClassException: sheet2_7.students; local class incompatible: 
    stream classdesc serialVersionUID = -4264328281487890061, 
    local class serialVersionUID = 397805898333518525 
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623) 
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) 
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) 
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at sheet2_7.Sheet2_7.main(Sheet2_7.java:81) 
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Mostafa M Awad
  • 321
  • 1
  • 2
  • 8
  • I can't read the image. It is too small and you appear to have chopped off some of the text of the message. Cut and paste the stacktrace as text. – Stephen C Mar 01 '15 at 03:29
  • Exception in thread "main" java.io.InvalidClassException: sheet2_7.students; local class incompatible: stream classdesc serialVersionUID = -4264328281487890061, local class serialVersionUID = 397805898333518525 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621) – Mostafa M Awad Mar 01 '15 at 03:36
  • at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at sheet2_7.Sheet2_7.main(Sheet2_7.java:81) Java Result: 1 – Mostafa M Awad Mar 01 '15 at 03:36
  • Sigh ... into the Question ... – Stephen C Mar 01 '15 at 03:55

2 Answers2

2

how can i Use EOFException to end the loop to.

What you have written should work. The break in the catch block should terminate the loop.

Alternatively, move the loop inside the try block. When the exception is thrown you will have terminated the loop before the catch block is executed.

this i try in main but give exception

and i cant correct that exception

Are you saying that you are getting a compilation error on EOFException? If so, check that you have imported it.

Otherwise, you will need to show us the complete stacktrace for the exception ... including the exception name and message.

UPDATE

The exception stacktrace is telling me that serialization signature of the serial objects does not match the signature of the class in your code. This most likely means that you have changed the class since you created the serialized objects.

This is one of the problems with using Java serialization in a data persistence scheme. (See What is a serialVersionUID and why should I use it?)

Without knowing how you got to this state, it is not possible to advise what the solution is.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

First, you need to allocate room for a student after you read it in. Second, you can use the Scanner class to check if there is anything on the next line before you process it.

alazar
  • 173
  • 7
  • This is wrong / unhelpful. 1) he doesn't need to allocate room if he is just printing out information like this. 2) Scanner is for text input. He is trying to read serialized objects. The representation is binary, not text. – Stephen C Mar 01 '15 at 03:21