javaBean的XML序列化和Object序列化的比较,以及读写文件效率

一.创建JavaBean
import java.io.Serializable;

public class Student implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3566427234289933929L;
	
	private int id;
	
	private String name;

	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 String toString(){
		return id+","+name;
	}
}
import java.io.Serializable;
import java.util.List;

public class Teacher implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8284383475131070516L;

	private String name;

	private List
     
     
      
       studentList;

	public String getName() {
		return name;
	}

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

	public List
      
      
       
        getStudentList() {
		return studentList;
	}

	public void setStudentList(List
       
       
        
         studentList) {
		this.studentList = studentList;
	}

}
       
       
      
      
     
     

public class UnserialBean {

	private int id;
	
	private String name;

	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;
	}
	
}

二.XML序列化和反序列化
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class XMLSerial {

	public static void writeObjectToFile(Object obj) throws FileNotFoundException {
		File targetFile = new File("xml/serial.xml");
		XMLEncoder xe = new XMLEncoder(new FileOutputStream(targetFile), "GBK", true, 0); // 仅用于Java
																							// SE
																							// 7
		xe.writeObject(obj); // 序列化成XML字符串
		xe.close();
	}

	public static Object readFromFile() throws FileNotFoundException {
		File targetFile = new File("xml/serial.xml");
		XMLDecoder xd = new XMLDecoder(new FileInputStream(targetFile));
		Object obj = xd.readObject(); // 从XML序列中解码为Java对象
		xd.close();
		return obj;
	}

}

三.Object序列化和反序列化
public class ObjectSerial {
	public static void writeObjectToFile(Object obj) {
		File file = new File("dat/test.dat");
		FileOutputStream out;
		try {
			out = new FileOutputStream(file);
			ObjectOutputStream objOut = new ObjectOutputStream(out);
			objOut.writeObject(obj);
			objOut.flush();
			objOut.close();
			System.out.println("write object success!");
		} catch (IOException e) {
			System.out.println("write object failed");
			e.printStackTrace();
		}
	}

	public static Object readObjectFromFile() {
		Object temp = null;
		File file = new File("dat/test.dat");
		FileInputStream in;
		try {
			in = new FileInputStream(file);
			ObjectInputStream objIn = new ObjectInputStream(in);
			temp = objIn.readObject();
			objIn.close();
			System.out.println("read object success!");
		} catch (IOException e) {
			System.out.println("read object failed");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return temp;
	}
}


四.比较两者通过文件读写速率
public static void main(String[] args) throws FileNotFoundException {
		List
    
    
     
      list = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			Student bean = new Student();
			bean.setId(i);
			bean.setName("niny");
			list.add(bean);
		}
		Teacher teacher = new Teacher();
		teacher.setStudentList(list);
		teacher.setName("Mike");

		Long time = System.currentTimeMillis();
		ObjectSerial.writeObjectToFile(teacher);
		System.out.println("success with ObjectSerial " + (System.currentTimeMillis() - time) + " ss");

		time = System.currentTimeMillis();
		XMLSerial.writeObjectToFile(teacher);
		System.out.println("success with write XMLSerial " + (System.currentTimeMillis() - time) + " ss");

		time = System.currentTimeMillis();
		ObjectSerial.readObjectFromFile();
		System.out.println("success with read ObjectSerial " + (System.currentTimeMillis() - time) + " ss");

		time = System.currentTimeMillis();
		XMLSerial.readFromFile();
		System.out.println("success with read XMLSerial " + (System.currentTimeMillis() - time) + " ss");

		UnserialBean bean = new UnserialBean();
		bean.setId(11);
		bean.setName("Liny");

		time = System.currentTimeMillis();
		XMLSerial.writeObjectToFile(bean);
		System.out.println("success with write unserial obj XMLSerial " + (System.currentTimeMillis() - time) + " ss");

		time = System.currentTimeMillis();
		ObjectSerial.writeObjectToFile(bean);
		System.out.println("success with write unserial obj ObjectSerial " + (System.currentTimeMillis() - time) + " ss");

	}
    
    

五.Console输出结果
success with ObjectSerial 93 ss
success with write XMLSerial 722 ss
read object success!
success with read ObjectSerial 123 ss
success with read XMLSerial 318 ss
success with write unserial obj XMLSerial 4 ss
write object failed
success with write unserial obj ObjectSerial 80 ss
java.io.NotSerializableException: com.hl.serial.bean.UnserialBean
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.hl.serial.ObjectSerial.writeObjectToFile(ObjectSerial.java:17)
at com.hl.serial.Main.main(Main.java:49)


六.总结
A.Object序列方式比较快,存储空间小
B.XML方式不需要Bean序列化,而Object序列化必须在Bean继承于Serializable接口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值