对象的序列化
import java.io.*;
public class Person11 implements Serializable
{
private String name;
int age;
Person11(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return name+":"+age;
}
}
import java.io.*;
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception
{
// writeObj();
readObj();
}
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person11 p = (Person11)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj() throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person11("lisi",39));
oos.close();;
}
}
//输出
lisi:39
将Person11中的内容改变,重新读取会报错。
import java.io.*;
public class Person11 implements Serializable
{
String name;
int age;
Person11(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return name+":"+age;
}
}
import java.io.*;
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception
{
// writeObj();
readObj();
}
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person11 p = (Person11)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj() throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person11("lisi",39));
oos.close();;
}
}
//输出
Exception in thread "main" java.io.InvalidClassException: Person11; local class incompatible: stream classdesc serialVersionUID = -4826887220003220322, local class serialVersionUID = 958454580975609894
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1885)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at ObjectStreamDemo.readObj(ObjectStreamDemo.java:13)
at ObjectStreamDemo.main(ObjectStreamDemo.java:7)
静态成员不能被序列化
import java.io.*;
public class Person11 implements Serializable
{
public static final long serialVersionUID = 42L;
private String name;
int age;
static String country = "cn";
Person11(String name,int age,String country)
{
this.name = name;
this.age = age;
this.country = country;
}
public String toString()
{
return name+":"+age+":"+country;
}
}
import java.io.*;
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception
{
// writeObj();
readObj();
}
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person11 p = (Person11)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj() throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person11("lisi0",399,"kr"));
oos.close();;
}
}
//输出
lisi0:399:cn
成员变量被transient修饰后,不能被序列化,保证其值在堆内存中存在,不在文本文件中存在。
import java.io.*;
public class Person11 implements Serializable
{
public static final long serialVersionUID = 42L;
private String name;
transient int age;
static String country = "cn";
Person11(String name,int age,String country)
{
this.name = name;
this.age = age;
this.country = country;
}
public String toString()
{
return name+":"+age+":"+country;
}
}
import java.io.*;
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception
{
// writeObj();
readObj();
}
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person11 p = (Person11)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj() throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person11("lisi0",399,"kr"));
oos.close();;
}
}
//输出
lisi0:0:cn
序列化多个对象,每读取一个对象,结束符自动标记到这个对象的末尾。再读取时,是下一个对象。
import java.io.*;
public class Person11 implements Serializable
{
public static final long serialVersionUID = 42L;
private String name;
int age;
static String country = "cn";
Person11(String name,int age,String country)
{
this.name = name;
this.age = age;
this.country = country;
}
public String toString()
{
return name+":"+age+":"+country;
}
}
import java.io.*;
public class ObjectStreamDemo {
public static void main(String[] args) throws Exception
{
// writeObj();
readObj();
}
public static void readObj() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person11 p = (Person11)ois.readObject();
System.out.println(p);
Person11 p0 = (Person11)ois.readObject();
System.out.println(p0);
ois.close();
}
public static void writeObj() throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person11("lisi0",399,"kr"));
oos.writeObject(new Person11("lisi0",299,"Am"));
oos.writeObject(new Person11("lisi0",199,"En"));
oos.writeObject(new Person11("lisi0",99,"Ge"));
oos.writeObject(new Person11("lisi0",9,"Fr"));
oos.close();;
}
}
//输出
lisi0:399:cn
lisi0:299:cn
网友评论