Parcelable
和Serializable
對象的序列化是把Java
對象轉化爲字節序列並存儲至一個存儲媒介(硬盤或者內存)的過程,反序列化則是把字節序列恢復爲Java
對象的過程,但它們僅處理Java
變量而不處理方法。數組
序列化的緣由:bash
Serializable
Serializable
Parcelable
兩種序列化的區別:網絡
Serializable
只須要對某個類以及它的屬性實現Serializable
接口便可,它的缺點是使用了反射,序列化的過程比較慢,這種機制會在序列化的時候建立許多的臨時對象,容易引起頻繁的gc
。Parcelable
是Android
平臺特有的,在使用內存的時候性能更好,但Parcelable
不能使用在要將數據存儲在磁盤的狀況下,由於Parcelable
不能很好的保證數據的持續性在外界有變化的狀況。Android
平臺上的應用intent
傳遞複雜對象intent
支持傳遞的數據類型包括:app
String/CharSequence
類型的數據、及其數組。Parcelable/Serializable
,及其數組/列表數據。SharePreference
存儲複雜對象Serializable
和Parcelable
Serializable
的讀寫操做首先定義咱們要序列化的對象。ide
public class SBook implements Serializable {
public int id;
public String name;
}
複製代碼
進行讀寫操做:性能
private void readSerializable() {
ObjectInputStream object = null;
try {
FileInputStream out = new FileInputStream(Environment.getExternalStorageDirectory() + "/sbook.txt");
object = new ObjectInputStream(out);
SBook book = (SBook) object.readObject();
if (book != null) {
Log.d(TAG, "book, id=" + book.id + ",name=" + book.name);
} else {
Log.d(TAG, "book is null");
}
} catch (Exception e) {
Log.d(TAG, "readSerializable:" + e);
} finally {
try {
if (object != null) {
object.close();
}
} catch (Exception e) {
Log.d(TAG, "readSerializable:" + e);
}
}
}
private void writeSerializable() {
SBook book = new SBook();
book.id = 1;
book.name = "SBook";
ObjectOutputStream object = null;
try {
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/sbook.txt");
object = new ObjectOutputStream(out);
object.writeObject(book);
object.flush();
} catch (Exception e) {
Log.d(TAG, "writeSerializable:" + e);
} finally {
try {
if (object != null) {
object.close();
}
} catch (Exception e) {
Log.d(TAG, "writeSerializable:" + e);
}
}
}
複製代碼
Parcelable
的讀寫操做定義序列化對象:ui
public class PBook implements Parcelable {
public int id;
public String name;
public PBook(int id, String name) {
this.id = id;
this.name = name;
}
private PBook(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Parcelable.Creator<PBook> CREATOR = new Parcelable.Creator<PBook>() {
@Override
public PBook[] newArray(int size) {
return new PBook[size];
}
@Override
public PBook createFromParcel(Parcel source) {
return new PBook(source);
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
}
}
複製代碼
寫入和讀取:this
private Intent writeParcelable() {
PBook tony = new PBook(1, "tony");
PBook king = new PBook(2, "king");
ArrayList<PBook> list = new ArrayList<>();
list.add(tony);
list.add(king);
Intent intent = new Intent();
intent.putParcelableArrayListExtra("PBook", list);
return intent;
}
private void readParcelable(Intent intent) {
if (intent != null) {
ArrayList<PBook> list = intent.getParcelableArrayListExtra("PBook");
if (list != null) {
for (PBook book : list) {
Log.d(TAG, "readParcelable, id=" + book.id + ", name=" + book.name);
}
}
}
}
複製代碼
SharePreference
存儲複雜對象//obejct -> ObjectOutputStream(ByteArrayOutputStream) -> ByteArrayOutputStream() -> byte[] -> String -> sp
private void writeSP() {
SBook book = new SBook();
book.id = 2;
book.name = "sp";
SharedPreferences sp = getSharedPreferences("SBookSP", MODE_PRIVATE);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ObjectOutputStream object = new ObjectOutputStream(os);
object.writeObject(book);
String base64 = new String(Base64.encode(os.toByteArray(), Base64.DEFAULT));
SharedPreferences.Editor editor = sp.edit();
editor.putString("SBook", base64);
editor.apply();
} catch (Exception e) {}
}
//sp -> string -> byte[] -> ByteArrayInputStream(byte[]) -> ObjectInputStream(ByteArrayInputStream) -> object
private void readSP() {
SharedPreferences sp = getSharedPreferences("SBookSP", MODE_PRIVATE);
String sbook = sp.getString("SBook", "");
if (sbook.length() > 0) {
byte[] base64 = Base64.decode(sbook.getBytes(), Base64.DEFAULT);
ByteArrayInputStream is = new ByteArrayInputStream(base64);
try {
ObjectInputStream object = new ObjectInputStream(is);
SBook book = (SBook) object.readObject();
if (book != null) {
Log.d(TAG, "readSP, id=" + book.id + ", name=" + book.name);
}
} catch (Exception e) {
}
}
}
複製代碼