1、Java的Serializable与Externalizable:
区别:
Serializable接口
优点:内建支持、易于实现
缺点:占用空间过大、额外的开销速度慢
Externalizable接口
优点:用户选择性序列化,开销较少
缺点:需用户选择控制序列化。
Serializable系统自动序列化对象状态(成员变量),transient,static变量不序列化;
Externalizable用户全权控制序列化过程(成员变量选择性序列化,可对变量先压缩加密再序列化),transient,static变量也可序列化
class Demo implements Serializable
{
int a;
transient int b;
static int c;
String d;
transient String e;
/**
* @author xiazehong
* @date 2014年12月4日 上午8:41:40
*/
public Demo(int a, int b, int c, String d, String e)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
}
class Demo implements Externalizable
{
int a;
transient int b;
static int c;
String d;
transient String e;
public Demo()
{
}
/**
* @author xiazehong
* @date 2014年12月4日 上午8:41:40
*/
public Demo(int a, int b, int c, String d, String e)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
@Override
public void writeExternal(ObjectOutput oo)
throws IOException
{
oo.writeInt(a);
oo.writeInt(b);
oo.writeInt(c);
oo.writeObject(d);
oo.writeObject(e);
}
@Override
public void readExternal(ObjectInput oi)
throws IOException, ClassNotFoundException
{
a = oi.readInt();
b = oi.readInt();
c = oi.readInt();
d = (String)oi.readObject();
e = (String)oi.readObject();
}
}
public class Main
{
public static void main(String[] args)
{
// try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/serial.txt")))
// {
// Demo demo = new Demo(1, 2, 3, "xzh", "cmm");
// oos.writeObject(demo);
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/serial.txt")))
{
Demo demo = (Demo)ois.readObject();
System.out.println("a=" + demo.a);
System.out.println("b=" + demo.b);
System.out.println("c=" + demo.c);
System.out.println("d=" + demo.d);
System.out.println("e=" + demo.e);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
2、Andriod的Parcelable
class Demo implements Parcelable
{
private int mData;
public int describeContents()
{
return 0;
}
public void writeToParcel(Parcel out, int flags)
{
out.writeInt(mData);
}
public static final Parcelable.Creator<Demo> CREATOR = new Parcelable.Creator<Demo>()
{
public Demo createFromParcel(Parcel in)
{
return new Demo(in);
}
public Demo[] newArray(int size)
{
return new Demo[size];
}
};
private Demo(Parcel in)
{
mData = in.readInt();
}
}
区别:
Serializable接口
优点:内建支持、易于实现
缺点:占用空间过大、额外的开销速度慢
Externalizable接口
优点:用户选择性序列化,开销较少
缺点:需用户选择控制序列化。
Serializable系统自动序列化对象状态(成员变量),transient,static变量不序列化;
Externalizable用户全权控制序列化过程(成员变量选择性序列化,可对变量先压缩加密再序列化),transient,static变量也可序列化
class Demo implements Serializable
{
int a;
transient int b;
static int c;
String d;
transient String e;
/**
* @author xiazehong
* @date 2014年12月4日 上午8:41:40
*/
public Demo(int a, int b, int c, String d, String e)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
}
class Demo implements Externalizable
{
int a;
transient int b;
static int c;
String d;
transient String e;
public Demo()
{
}
/**
* @author xiazehong
* @date 2014年12月4日 上午8:41:40
*/
public Demo(int a, int b, int c, String d, String e)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
@Override
public void writeExternal(ObjectOutput oo)
throws IOException
{
oo.writeInt(a);
oo.writeInt(b);
oo.writeInt(c);
oo.writeObject(d);
oo.writeObject(e);
}
@Override
public void readExternal(ObjectInput oi)
throws IOException, ClassNotFoundException
{
a = oi.readInt();
b = oi.readInt();
c = oi.readInt();
d = (String)oi.readObject();
e = (String)oi.readObject();
}
}
public class Main
{
public static void main(String[] args)
{
// try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/serial.txt")))
// {
// Demo demo = new Demo(1, 2, 3, "xzh", "cmm");
// oos.writeObject(demo);
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/serial.txt")))
{
Demo demo = (Demo)ois.readObject();
System.out.println("a=" + demo.a);
System.out.println("b=" + demo.b);
System.out.println("c=" + demo.c);
System.out.println("d=" + demo.d);
System.out.println("e=" + demo.e);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
2、Andriod的Parcelable
class Demo implements Parcelable
{
private int mData;
public int describeContents()
{
return 0;
}
public void writeToParcel(Parcel out, int flags)
{
out.writeInt(mData);
}
public static final Parcelable.Creator<Demo> CREATOR = new Parcelable.Creator<Demo>()
{
public Demo createFromParcel(Parcel in)
{
return new Demo(in);
}
public Demo[] newArray(int size)
{
return new Demo[size];
}
};
private Demo(Parcel in)
{
mData = in.readInt();
}
}