the method of the Flow action object:
ObjectInputStream and ObjectOutputStream two objects, objects saved with ObjectOutputStream can only be read with ObjectInputStream serializable serialization:
When each class implements serialiable serialization, Java generates a UID based on the class's member variables and member functions to differentiate each of the different classes, and of course, you can set this UID yourself manually.
The method is set to: Static final long serialversionuid = 42L; serializable function:
When a class implements the serializable, the ObjectOutputStream is used to store objects created by this class in a file, which generates a UID corresponding to this class, and the next time, with ObjectInputStream
When you read an object in this file, it matches the same UID class. If the original class changes, it will generate a new UID, at this point, read the object in the file, it will error classnotfoundexception; Of course, this
, we can also fix the UID of the class, which can be read even if the corresponding class of the object in the file is changed. Serializable scope:
Only objects stored in the heap memory can be serialized. If you are a static member stored in a static area, it cannot be serialized. Of course, if you have a member that you don't want to be serialized, you can add the Transient keyword before the member.
Here's an example of a stream manipulation object:
Import java.io.*;
Class Objectstreamdemo
{public
static void Main (string[] args) throws IOException
{
readobj ();
}
//Storage object to file public
static void Writeobj () throws IOException
{
//Create ObjectOutputStream object
ObjectOutputStream oos=new ObjectOutputStream (New FileOutputStream ("Object.txt"));
Oos.writeobject (New person ("Enen", 2));
Oos.close ();
}
Read and print the objects in the file to public
static void Readobj () throws IOException
{
//Create ObjectInputStream read object stream
ObjectInputStream ois=new ObjectInputStream (New FileInputStream ("Object.txt"));
Person P=null;
Try
{
p= (person) ois.readobject ();
}
catch (ClassNotFoundException e)
{
throw new RuntimeException (e.tostring ());
}
SYSTEM.OUT.PRINTLN (P);
}
Import java.io.*;
Class Person implements Serializable
{
String name;
int age;
Person (String Name,int age)
{
this.name=name;
this.age=age;
}
Public String toString ()
{return
name+ "::" +age;
}
}