Current location - Quotes Website - Personality signature - Exception in thread "main" java.io. Eofexception occurred in Java.
Exception in thread "main" java.io. Eofexception occurred in Java.
/*?

I study java, too. I have looked up the information for a long time, and I probably understand what is going on. Sort out the train of thought, as follows:

The ObjectInputStream and ObjectOutputStream classes can read objects. ObjectOutputStream requires that the written object must implement the Serialiable interface, while ObjectInputStream is used to read the previous object. ObjectOutputStream? The written basic data and objects are deserialized. Let's not talk about writing, but use the readObject () method when reading. ?

Method signature: public? Final? Object? ReadObject () throws? IOException,ClassNotFoundException? Caption: When? InputStream? An exception is thrown when there is a problem or a class that should not be deserialized. All the anomalies are right? InputStream? Are fatal, will make it in an uncertain state; Whether to ignore or restore the stream state depends on the caller. Throw ClassNotFoundException, InvalidClassException, StreamCorruptedException, OptionalDataException and IOExcepiton (you can see api documents specifically), where does EOFExcepiton come from and find the source code. Read object-"Read object 0-" peeks at bytes, and takes the code by the way: (here is EOFException, you can skip it directly if you don't mind) */

Bytes? peekByte()? Throwing? IOException? {

int? Val. =? peek();

What if? (Val? & lt? 0)? {

Throw? New? eof exception();

}

Return? (bytes)? val

}?

/*

At this point, the program throws an EOFException, which means that the stream has reached the end and you try to continue the readObject operation. In this question, it is while(obj! =null), the return value of the readObject method is Object, and the return value is null when end is not specified (that is, it cannot be obj! =null to determine whether there is an object in the future), what should I do?

At first, I thought of the () method available in this class, which is while (dis). available()>; 0) As a condition, the result is the same as EOFException. After testing, it is found that the return value of the dis.available () method is always 0, which is the case with files written with ObjectOutputStream. The (available () method also has instructions: Returns? That? Number? Yes? Bytes? That? Is it okay? Is it? Reading? No? Block. Have you noticed? Blocked). Then what? Looking up the information on the internet, there are three ways to solve this problem:

1, using container classes to store objects, such as List, StringBuffered, etc. Write and read once, and add and modify objects through container classes.

2. Write one more variable in the file, such as int? Length? Used to record how many objects enter, through while(length >;; 0), but the length should be modified when adding or deleting objects in the file.

3. Judge that you have reached the end of the file by capturing EOFExceptin (clever thinking). ?

Here is your code, in while () and catch(EOFException? E) Block, nothing else has changed (classes School and Student2 were added by myself according to your code, which may be different from yours). Running results:

Student 2? [name = Zhang Sa,? id=30]

Student 2? [name=mnc,? id=30]

Have you reached the end of the document?

*/

Import? Java . io . eof exception;

Import? Java . io . file;

Import? Java . io . file inputstream;

Import? Java . io . file output stream;

Import? Java . io . io exception;

Import? Java . io . objectinputstream;

Import? Java . io . object output stream;

Import? Java . io . random access file;

Import? Java . io . serializable;

Import? Java . util . ArrayList;

Import? Java . util . iterator;

Import? Java . util . list;

Class? School {

Private? String? Name;

Public? List & lt students & gt? List;

Public? School (string? Name)? {

This name? =? Name;

}

Public? List & lt students & gt? getAllStudents()? {

If (list? ==? Empty)

List? =? New? ArrayList & lt student & gt ();

Return? List;

}

}

Class? Student 2? Artifacts? Serializable {

/**

*?

*/

Private? Static electricity Final? Dragon? serialVersionUID? =? 1L;

Private? String? Name;

Private? int? id;

Public? Student2 (string? Name,? int? id)? {

This name? =? Name;

This. id? =? id;

}

@ Overlay

Public? String? toString()? {

Return? "Student 2? [name= "? +? Name? +?" ,? id= "? +? id? +?" ]";

}

}

Public? Class? Test0 1? {

Public? Static electricity Invalid? Main (string? args[])? Throwing? Abnormal {

Documents? F = new? file(" d:"+file . separator+" test 2 . txt "); ?

//? RandomAccessFile? rdf=new? RandomAccessFile(f,“rw”);

School? sch? =? New? School ("Southwest University");

Student 2? S 1 = new? Student2 ("Zhang Sa", 30);

Student 2? S2 = new? Student 2 ("MNC", 30); ?

sch.getAllStudents()。 Add (s1); ?

sch.getAllStudents()。 Add (S2); ?

Iterator & lt student & gt? iter=sch.getAllStudents()。 Iterator ();

ObjectOutputStream? oos=? null? ;

oos? =? New? ObjectOutputStream (new? FileOutputStream(f))? ;

while(iter.hasNext())

{

Student 2? a5 = ITER . next();

oos.writeObject(a5)? ; ?

}?

Object? obj? =? null? ; ? //? Receiving the read content

ObjectInputStream? ois? = null

Try {?

ois? =? New? ObjectInputStream (new? FileInputStream(f))? ; ? //? Instantiate the object input stream

While(true)// Use while (true) directly to continue the reading operation.

{

obj? =? ois . read object(); ? //Cycle the following data

system . out . println(obj);

}

}catch(EOFException? e){? //capture EOFException here?

System.out.println ("End of file reached");

}catch(IOException? e){

e . printstacktrace();

}

Finally {?

If (ois! = empty)

{?

ois.close()? ; ? //? close

}?

If (oos! = empty)

OOS . close();

}

}?

}