My Java Development learning journey------>java using ObjectOutputStream and ObjectInputStream serial Number object related problem solving methods

Source: Internet
Author: User
Tags object serialization

Today, ObjectOutputStream and ObjectInputStream are used for object serialization, and the Java.io.EOFException exception is reported.

The exception code is as follows:

Java.io.EOFExceptionat Java.io.objectinputstream$blockdatainputstream.peekbyte (objectinputstream.java:2554) at Java.io.ObjectInputStream.readObject0 (objectinputstream.java:1297) at Java.io.ObjectInputStream.readObject ( objectinputstream.java:351) at Cn.fuxi.io.ReadObject.readObject (readobject.java:27) at Cn.fuxi.io.ReadObject.main ( READOBJECT.JAVA:12)
This exception is because I useObjectInputStream Read the object, did not determine whether the ObjectInputStream read to the unknown length of the end of the file, resulting in the end of the file, the program does not read the contents of the file normally.


Later, after a study, three kinds of rescue ideas are drawn:

The first method:

After writing the object, add a sentence oos.writeobject (null); Inserting null is used to determine whether to read to the end. Oos is ObjectOutputStream instance.

Then, when the object is read, use the while ((obj = Ois.readobject ()) = null) to determine whether to read an object again, determine if the object is empty, if it is not NULL to continue reading, or NULL to stop reading.


The second method:

A container (such as ArrayList) is loaded into a container (for example, a variable number of objects), and then written to the object when it is written.

When reading, read out the container and then traverse the container to remove the object that you want.


A third method:

Similar to the second method, the objects are stored in an array and then written to the array object.

When reading, remove the array, then iterate over the array, and remove the desired object.


Here are the specific code actions:


1, the object to be serial number Person.java


Import Java.io.serializable;public class Person implements Serializable {/** *  */private static final long serialvers Ionuid = -6374324573857634276l;private string name;private int age;public person (String name, int age) {this.name = Name;t His.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

2. Three ways to write to an object

Import Java.io.fileoutputstream;import java.io.objectoutputstream;import Java.util.arraylist;import java.util.List ;p Ublic class WriteObject {/** * @param args */public static void main (string[] args) {Writeobjectbyarray (); WriteObject (); Writeobjectbylist ();} /** * Write directly to the object */private static void WriteObject () {try {objectoutputstream oos = new ObjectOutputStream (New Fileoutputstrea M ("Object.txt")), for (int i = 1; i < i++) {oos.writeobject ("Ouyangpeng [" + i+ "]", I));} Inserting null is used to determine if reading to the end//write-end flag is convenient to read (very important, if you do not write, the reading can not be located at the end of the read);//Read the time will be reported java.io.EOFException abnormal oos.writeobject ( NULL); Oos.close ();} catch (Exception e) {e.printstacktrace ();}} /** * Writes an object through an array */private static void Writeobjectbyarray () {try {objectoutputstream oos = new ObjectOutputStream (New Fileou Tputstream ("ObjectByArray.txt")); person[] persons = new Person[10];for (int i = 1; i < i++) {person person = new Person ("Ouyangpeng arrays[" + (10+i) + "]", 10+i);p ersons[i] = person;} Oos.writeobject (persons); Oos.close ();} Catch (Exception e) {E.printstacktrace ();}} /** * Writes an object through a collection */private static void Writeobjectbylist () {try {objectoutputstream oos = new ObjectOutputStream (New Fileout Putstream ("ObjectByList.txt")); List<person> persons=new arraylist<person> (); for (int i = 1; i <, i++) {Person person = new person ("Ouyangpeng list["+ (20+i) +"] ", 20+i);p ersons.add (person); Write Listoos.writeobject (persons); Oos.close ();} catch (Exception e) {e.printstacktrace ();}}}


3. Three ways to read objects

Import Java.io.fileinputstream;import Java.io.objectinputstream;import Java.util.list;public class ReadObject {/** * @ param args */public static void main (string[] args) {readobject (); System.out.println ("=============================="); Readobjectbyarrays (); System.out.println ("=============================="); Readobjectbylist ();} /** * Direct Read object */private static void ReadObject () {try {objectinputstream ois = new ObjectInputStream ("O Bject.txt ")); object obj = null;//If NULL is read to end of file//Read end flag bit: Reads an object again, determines whether the object is empty, if it is not NULL to continue reading if NULL is stopped reading while (obj = Ois.readobject ()) = null) {////deserialization read gets personperson person = (person) obj; System.out.println ("Name:" + person.getname () + "Age:" + person.getage ());} Ois.close ();} catch (Exception e) {e.printstacktrace ();}} /** * Reads an object through an array */private static void Readobjectbyarrays () {try {objectinputstream ois = new ObjectInputStream (New Fileinpu TStream ("ObjectByArray.txt"));//deserialization read get person[]person[] persons = (person[]) ois.readobject (); for (int i = 1; i < persons.length; i++) {Person person = (person) persons[i]; System.out.println ("Name:" + person.getname () + "Age:" + person.getage ());} Ois.close ();} catch (Exception e) {e.printstacktrace ();}} /** * Read objects through a collection */private static void Readobjectbylist () {try {objectinputstream ois = new ObjectInputStream (New fileinputs Tream ("ObjectByList.txt"));//deserialization reads get list<person>list<person> persons = (list<person>) Ois.readobject (); for (int i = 0; I <persons.size (); i++) {person person=persons.get (i); System.out.println ("Name:" + person.getname () + "Age:" + person.getage ());} Ois.close ();} catch (Exception e) {e.printstacktrace ();}}}


Here's the result:

After writing the object, in the SRC sibling directory, generate 3 files, respectively Object.txt, ObjectByArray.txt, objectByList.txt as shown.



Read the object with the following result:

The name is: Ouyangpeng [1]  Age: 1 Name: Ouyangpeng [2] Age:  2 Name: Ouyangpeng [3]  Age: 3 Name: Ouyangpeng [4]  Age: 4 Name: Ouyangpeng [5]  Age: 5 Name: Ouyangpeng [6]  Age: 6 Name: Ouyangpeng [7]  Age: 7 Name: Ouyangpeng [8]  Age: 8 Name: Ouyangpeng [9]  Age: 9============================== Name: Ouyangpeng ARRAYS[11]  Age: 11 Name: Ouyangpeng arrays[12]  Age: 12 Name: Ouyangpeng arrays[13]  Age: 13 Name: Ouyangpeng arrays[14]  Age: 14 Name: Ouyangpeng arrays[15]  Age: 15 Name: Ouyangpeng arrays[16]  Age: 16 Name: Ouyangpeng arrays[17]  Age: 17 Name: Ouyangpeng arrays[18]  Age: 18 Name: Ouyangpeng arrays[19]  Age: 19============================== Name: Ouyangpeng list[21]  Age: 21 Name: Ouyangpeng list[  Age: 22 characters: Ouyangpeng list[23]  Age: 23 Name: Ouyangpeng list[24]  Age: 24 Name: Ouyangpeng list[25]  Age: 25 Name: Ouyangpeng list[26 ]  Age: 26 Name: Ouyangpeng list[27]  Age: 27 Name: Ouyangpeng list[28]  Age: 28 Name: Ouyangpeng list[29]  Age: 29


determine if ObjectInputStream read to end of file, file length unknowndetermine if ObjectInputStream read to end of file, file length unknowndetermine if ObjectInputStream read to end of file, file length unknown

My Java Development learning journey------>java using ObjectOutputStream and ObjectInputStream serial Number object related problem solving methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.