您的位置:首页 > 编程语言 > Java开发

java:序列化&反序列化 自定义序列化的优点

2017-02-28 17:19 375 查看
自定义序列化可以提高性能,例如ArrayList中的自定义序列化

ArrayList中的自定义序列化,可以提高性能,优化数组存储,取出
rivate void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();

// Write out size as capacity for behavioural compatibility with clone()
s.writeInt(size);

// Write out all elements in the proper order.
for (int i=0; i<size; i++) {
s.writeObject(elementData[i]);
}

if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;

// Read in size, and any hidden stuff
s.defaultReadObject();

// Read in capacity
s.readInt(); // ignored

if (size > 0) {
// be like clone(), allocate array based upon size not capacity
ensureCapacityInternal(size);

Object[] a = elementData;
// Read in all elements in the proper order.
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}
可以看到读取时,只是读取有效的进行序列化。存储时也是取有效数组中的元素进行序列化

package cn.java;

import java.io.Serializable;
import java.util.ArrayList;

public class Student implements Serializable{

private int id;
private String name;
//transient表示不会被序列化,但是可以自定义序列化
private transient int age;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}

public Student() {
super();
}

@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();//默认的序列化
this.age = s.readInt();//自定义序列化
}

private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
s.defaultWriteObject();//默认的序列化
s.writeInt(this.age);
}
}
package cn.java;

import java.io.Fi
a15f
leInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
* 序列话反序列话操作 2017年2月28日16:57:30
* @author admin
*
*/
public class SerTest {

public static void main(String[] args) throws Exception {
a("demo\\l.dat");
b("demo\\l.dat");
}

//序列化操作
public static void a(String file) throws Exception{
ObjectOutputStream oop = new ObjectOutputStream(
new FileOutputStream(file));
Student stu = new Student(1, "刘炳旭", 22);
oop.writeObject(stu);
oop.flush();
oop.close();
}

//反序列化
public static void b(String file)throws Exception{
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(file));
Student stu = (Student)ois.readObject();
System.out.println(stu);
ois.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: