1.为什么要用序列化?
1. 在数据传输的时候,对象需要以二进制的字节流的形式来进行传送。
2. 如果对象长时间没有调用,那么可以通过序列化的方式来将对象存储在本地磁盘上,需要的时候再反序列化,这样可以节省服务器的资源。
2. 代码实现序列化
建立实体类需要实现 Serializable接口,并添加唯一的版本号标识。
实体类 student:
package com.test.day02;
import java.io.Serializable;
public class Student implements Serializable  {
	
	
	
	/**
	 * 唯一标识
	 */
	private static final long serialVersionUID = 1L;
	
	String name;
	int age;
	String address;
    Student(String name){
    	this(name,0,null);
    	
    }
    public Student(String name,int age){
    	this(name,age,null);
    	
    }
    Student(String name,int age,String address)
    {
     this.name=name;
     this.age=age;
     this.address=address;
    }
    void study(){
    	System.out.println(name+"在学习。。。。。");
    }
    void sayHai(){
    	System.out.println("我叫"+name+"我今年"+age+"岁了,我家住在"+address);
    }
    
    
}
测试类:
序列化使用ObjectOutputStream的os.writeObject(T t), 将对象转换为二进制字节流,反序列化使用ObjectInputStream的is.readObject(),将二进制字节流转换为对象,反序列化时返回的是Object类型,因此需要强制类型转换为对应的对象类型。
package com.test.day05;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.test.day02.Student;
public class Demo10 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Student stu1 = new Student("a",12);
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        System.out.println(stu1);
        try {
        	File file1=new File("D:/test/stu.txt");
        	if (!file1.exists()) {
        		file1.createNewFile();
			}
            oos = new ObjectOutputStream(new FileOutputStream(file1));
            oos.writeObject(stu1);
            oos.writeObject(stu1);
            
            ois = new ObjectInputStream(new FileInputStream(file1));
            Student stu2 = (Student) ois.readObject();
            Student stu3 = (Student) ois.readObject();
            //Student stu4 = (Student) ois.readObject();
            System.out.println(stu2);
            System.out.println(stu3);
            //读取出来的时候也是从前往后按照顺序读取。
            //System.out.println(stu4);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (oos != null) {
                oos.close();
            }
            if(ois!= null){
                ois.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
打印结果:
com.test.day02.Student@6d06d69c
com.test.day02.Student@3b07d329
com.test.day02.Student@3b07d329
由打印结果可知: 序列化后的重新生成了一个新的地址。
 
                  
                  
                  
                  
                            
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
                    
              
            
                  
					4015
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
					
					
					


            