序列化

序列化:

(1)概念:将内存中的java对象保存到文件中的过程称为序列化,

(2)步骤:

//1、将需要保存的对象的所在类实现序列化接口:Serializable

Class Student implements Serializable{}

//2、创建序列化对象: ObjectOutputStream

FileOutputStream fos = new FileOutputStream("文件路径");

ObjectOutputStream oos = new ObjectOutputStream(out);

//3、调用序列化方法

oos.writeObject(对象名);

//4、关闭流

Oos.close();

Out.close();

注:序列化在开发中经常使用,所以我们定义完一个实体类,就要他实现序列化接口

 

提到这个 序列化,还有几点要说明,就是 在后期我们做web开发的时候,如果长时间启动服务器做调试的话,会报一个 没有序列化的异常,需要重启tomcat服务器重新进行调试。【//这里面的说明未必对。这是因为,开始 还是使用字符流的方式进行java对象到文件的保存,也就是内存充足的情况下,运行了一段时间以后,空间不足,tomcat服务器帮我们自动以 一个 更加节省内存方式的方法来对数据进行存储,这样就会报一个为序列化的异常,这个时候只要重新启动服务器就可以解决问题。】

 

序列化听起来好像很高大上,但是其实就是众多流中的一种。对象流。也就是 objectoutputstream

 

package IOPart;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializationDemo {
    public static void main(String[] args) {
        
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream  = new ObjectOutputStream(new FileOutputStream("f:\\tryFile\\students.obj"));
            Student student1 = new Student("张三", 18, Gender.男, "一班");
            Student student2 = new Student("李飞", 24, Gender.男, "二班");
            objectOutputStream.writeObject(student1);
            objectOutputStream.writeObject(student2);
            objectOutputStream.flush();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(objectOutputStream!=null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("写出完成");
        
    }

}

枚举形式的性别:

package IOPart;

public enum Gender {
    男,女
}

学生对象:

package IOPart;

import java.io.Serializable;

public class Student implements Serializable {
    
    private String studentName;
    private int studentAge;
    private Gender studentGender;
    private String studentClazz;
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getStudentAge() {
        return studentAge;
    }
    public void setStudentAge(int studentAge) {
        this.studentAge = studentAge;
    }
    public Gender getStudentGender() {
        return studentGender;
    }
    public void setStudentGender(Gender studentGender) {
        this.studentGender = studentGender;
    }
    public String getStudentClazz() {
        return studentClazz;
    }
    public void setStudentClazz(String studentClazz) {
        this.studentClazz = studentClazz;
    }
    @Override
    public String toString() {
        return "Student [studentName=" + studentName + ", studentAge=" + studentAge + ", studentGender=" + studentGender
                + ", studentClazz=" + studentClazz + "]";
    }
    public Student(String studentName, int studentAge, Gender studentGender, String studentClazz) {
        super();
        this.studentName = studentName;
        this.studentAge = studentAge;
        this.studentGender = studentGender;
        this.studentClazz = studentClazz;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
    

}

这个 东西可以试着打开看一下,应该是 看不懂的。囧,毕竟这是给计算机看的,或者 给java程序看的,里面封装了一些二进制形式的数据,其实 多少还有点儿 不打算 让我们看懂的意思呢。

接下来,就是借助 objectInputStream把 刚刚写入的对象拿出来。【下面这里就是有弊端,存几个 放几个,拿几个,都要做到心里有数,才行,但是实际上,并不一定会知道后面到底还有没有元素,所以如果没有的话,可以通过,判定取到的结果为空,判定没有下一个元素,这或许是一个好的解决方式。】

把文件形式的对象在反馈成java代码的方式就是反序列化:

反序列化:

(1)概念:将对象从文件中读取到程序中的过程,成为反序列化

(2)步骤:

//将读取的对象所在的类实现序列化接口:Serializable

Class Student implements Serializable

//2、创建反序列化对象:ObjectInputsStream

FileinputStream fis = new FileInputStream("路径");

ObjectInputStream ois = new ObjectInputStream(fis);

//3、调用反序列化方法

Student s =(Student)ois.readObject();

注:反序列化时需要将读取的对象进行强转

//4、关流

Ois.close();

Fis.close();

 

 

package IOPart;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationDemo {
    public static void main(String[] args) {
        //writePart();
        readPart();
    }

    private static void readPart() {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("f:\\tryFile\\students.obj"));
            Student student = (Student) objectInputStream.readObject();
            System.out.println(student);
            Student s2 = (Student) objectInputStream.readObject();
            System.out.println(s2);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(objectInputStream!=null){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    private static void writePart() {
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream  = new ObjectOutputStream(new FileOutputStream("f:\\tryFile\\students.obj"));
            Student student1 = new Student("张三", 18, Gender.男, "一班");
            Student student2 = new Student("李飞", 24, Gender.男, "二班");
            objectOutputStream.writeObject(student1);
            objectOutputStream.writeObject(student2);
            objectOutputStream.flush();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(objectOutputStream!=null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("写出完成");
        
    }

}

随后遇到一个小bug,就是在 读完两个对象以后,当我想要读第三个对象的时候,【当然我就只写了两个对象】,报了EOFException。所以并没有报空指针,而是已经读到了文件结尾,那么采用了如下的解决方案,【EOFException属于IOException,所以,要把EOFexception放在 IOException的上面。】

 

package IOPart;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationDemo {
    public static void main(String[] args) {
        //writePart();
        readPart();
    }

    private static void readPart() {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("f:\\tryFile\\students.obj"));
            Student student = (Student) objectInputStream.readObject();
            System.out.println(student);
            Student s2 = (Student) objectInputStream.readObject();
            System.out.println(s2);
            Student s3 = (Student) objectInputStream.readObject();
            System.out.println(s3);
            
        } catch (EOFException e){
            System.out.println("已经读到文件结尾");
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(objectInputStream!=null){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    private static void writePart() {
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream  = new ObjectOutputStream(new FileOutputStream("f:\\tryFile\\students.obj"));
            Student student1 = new Student("张三", 18, Gender.男, "一班");
            Student student2 = new Student("李飞", 24, Gender.男, "二班");
            objectOutputStream.writeObject(student1);
            objectOutputStream.writeObject(student2);
            objectOutputStream.flush();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(objectOutputStream!=null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("写出完成");        
    }
}

 

posted on 2016-02-07 11:25  木鸟飞  阅读(211)  评论(0编辑  收藏  举报

导航