The ObjectInputStream and ObjectOutputStream classes are subclasses of the InputStream and OutputStream classes respectively. The objects created by the ObjectInputStream and ObjectOutputStream classes are called object input streams and object output streams . The object output stream uses the writeObject(Object obj) method to write an object obj to a file, and the object input stream uses readObject() to read an object into the program.
The construction methods of the ObjectInputStream and ObjectOutputStream classes are as follows:
The pointer of ObjectOutputStream should be an output stream object, so when preparing to write an object to a file, first create an output stream using a subclass of OutputStream.
For example, use FileOutputStream to create a file output stream, the code is as follows:
FileOutputStreamfileOut=newFileOutputStream(tom.txt);ObjectOutputStreamobjectout=newObjectOutputStream(fileOut);
Similarly, the pointer of ObjectInputStream should be an input stream object, so when preparing to read an object from a file into the program, first create an input stream using a subclass of InputStream.
For example, use FileInputStream to create a file input stream, the code is as follows:
FileInputStreamfileIn=newFileInputStream(tom.txt);ObjectInputStreamobjectIn=newObjectInputStream(fileIn);
When using object streams to write or read objects, ensure that the objects are serialized. This is to ensure that the objects can be written to the file and the objects can be read back to the program correctly.
If a class implements the Serializable interface (the interface in the java.io package), then the object created by this class is the so-called serialized object. Most of the objects provided by Java class libraries are so-called serialized. It should be emphasized that there are no methods in the Serializable interface, so classes that implement this interface do not need to implement additional methods. Another thing to note is that when using object streams to write an object to a file, not only must the object be serialized, but the member objects of the object must also be serialized.
The methods in the Serializable interface are invisible to the program, so classes that implement this interface do not need to implement additional methods. When a serialized object is written to the object output stream, the JVM will implement the methods in the Serializable interface. Write text in a certain format (serialization information of the object) to the destination. When the ObjectInputStream object stream reads an object from a file, the serialization information of the object is read back from the file, and an object is created based on the serialization information of the object.
For example, use object streams to read and write objects created by the TV class:
TV.java
importjava.io.*;publicclassTVimplementsSerializable{Stringname;intprice;publicvoidsetName(Strings){name=s;}publicvoidsetPrice(intn){price=n;}publicStringgetName(){returnname;}publicintgetPrice(){returnprice;}}
Main.java
importjava.io.*;publicclassMain{publicstaticvoidmain(Stringargs[]){TVchanghong=newTV();changhong.setName(Changhong TV);changhong.setPrice(5678);Filefile=newFile(television.txt);try{FileOutputStreamfileOut=newFileOutputStream (file);ObjectOutputStreamobjectOut=newObjectOutputStream(fileOut);objectOut.writeObject(changhong);objectOut.close();FileInputStreamfileIn=newFileInputStream(file);ObjectInputStreamobjectIn=newObjectInputStream(fileIn);TVxinfei=(TV)objectIn.readObject();objectIn .close();xinfei.setName(Xinfei TV);xinfei.setPrice(6666);System.out.println(changhong’s name:+changhong.getName());System.out.println(changhong’s price:+ changhong.getPrice());System.out.println(xinfei's name:+xinfei.getName());System.out.println(xinfei's price:+xinfei.getPrice());}catch(ClassNotFoundExceptionevent){System .out.println(cannot read object);}catch(IOExceptionevent){System.out.println(event);}}}