java序列化

1.



package com.zh.serializable;

import java.io.*;

import java.util.*;

public class MySerializable {



 /**序列化与反序列化的运用,序列化就是把一个对象写到一个输入流中

  * 反序列化就是反过来

  * @param args

  * @throws IOException 

  * @throws Exception 

  */

 public static void main(String[] args) throws Exception {

  // TODO Auto-generated method stub

  //使用对象输出流

  ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("D:/abc.txt"));

  String obj1="hello";

  Date obj2=new Date();

  Customer obj3=new Customer("tom", 20);

  //序列化对象

  out.writeObject(obj1);

  out.writeObject(obj2);

  out.writeObject(obj3);

  out.close();

  //反序列化

  ObjectInputStream in=new ObjectInputStream(new FileInputStream("D:/abc.txt"));

  String obj11=(String)in.readObject();

  System.out.println("objll:"+obj11);

  System.out.println("objll==obj1:"+(obj11==obj1));

  System.out.println("obj11.equals(obj11):"+obj11.equals(obj11));

  

  Date obj22=(Date)in.readObject();

  System.out.println("obj22:"+obj22);

  System.out.println("obj2==obj22:"+(obj2==obj22));

  System.out.println("obj22.equals(obj2):"+obj2.equals(obj22));

  

  Customer obj33=(Customer)in.readObject();

  System.out.println("obj33:"+obj33);

  System.out.println("obj3==obj33:"+(obj3==obj33));

  System.out.println("obj3.equals(obj33):"+obj3.equals(obj33));

  System.out.println(obj33.getAge());

  

  in.close();

 }

}

//要想对象被序列化 就要继承Serializable

class Customer implements Serializable{

 private String name;

 private int age;

 

 public Customer(String name, int age) {

  this.name = name;

  this.age = age;

  System.out.println("call second constructor");

 }

 

 public boolean equals(Object o){

  if(this == o){

   return true;

  }

  if(! (o instanceof Customer)){

   return false;

  }

  final Customer other=(Customer)o;

  if(this.name.equals(other.name) && this.age==other.age){

   return true;

  }else{

   return false;

  }

 }

 

 

 public String toString(){

  return "name="+name+",age"+age;

 }



 public String getName() {

  return name;

 }



 public int getAge() {

  return age;

 }



 public void setName(String name) {

  this.name = name;

 }



 public void setAge(int age) {

  this.age = age;

 }

 

}







2.



package com.zh.serializable;



import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;



public class MySerializable1 implements Serializable{



 /**

  * 用transient修饰可以不被序列化 因为在序列化后,容易被文件读取或拦截网络传输数据而

  * 偷窥

  * @param args

  */

 private String name;

 private transient String password;

 

 public MySerializable1(String name, String password) {

  super();

  this.name = name;

  this.password = password;

 }

 public String toString(){

  return name+" "+password;

 }

 public static void main(String[] args) throws IOException, ClassNotFoundException {

  // TODO Auto-generated method stub

  MySerializable1 user=new MySerializable1("zhouhai", "123456");

  System.out.println("before serialization: "+user);

  ByteArrayOutputStream buf=new ByteArrayOutputStream();

  

  //把user对象序列化到一个字节缓存中

  ObjectOutputStream o=new ObjectOutputStream(buf);

  o.writeObject(user);

  

  ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));

  user=(MySerializable1)in.readObject();

  System.out.println("after serialization: "+user);

 }



}

 

你可能感兴趣的:(java序列化)