首页 > 代码库 > java 序列化与反序列化

java 序列化与反序列化

package open_exe;

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

public class xuliehua {
    public void xulie(){
        Student st=new Student();
        st.setName("测试1");
        st.setAge(18);
        st.setSex("男");
        st.setHeight("168cm");
        File file= new File("C:"+File.separator+"file1"+File.separator+"student.txt"); 
          try  
          {  
              if(!file.getParentFile().exists()){//判断文件夹在不在  
                  file.getParentFile().mkdirs();//文件夹不在 创建文件夹
              }if(!file.exists()){//判断txt文件在不在 
                  file.createNewFile();//txt不在就创建一个txt
              }
          }  
          catch(IOException e)  
          {  
           e.printStackTrace();  
          }  
          try  
          {  
           //Student对象序列化过程  
           FileOutputStream fos = new FileOutputStream(file);  
           ObjectOutputStream oos = new ObjectOutputStream(fos);//序列化
           oos.writeObject(st);//写入txt文件
           oos.flush();  
           oos.close();  
           fos.close(); 
           System.out.println("完成系列化!");
          }    
          catch (IOException e)  
          {  
           e.printStackTrace();
           System.out.println("系列化失败!");
          } 
    }
public void fan(){
    File file = new File("C:\\file\\student.txt");   
      try  
      {  
       //Student对象序列化过程  
          FileInputStream fis = new FileInputStream(file);  
           ObjectInputStream ois = new ObjectInputStream(fis);//反序列化
           Student st1 = (Student) ois.readObject();
           System.out.println("name = " + st1.getName());  
           System.out.println("sex = " + st1.getSex());    
           ois.close();  
           fis.close();
           System.out.println("反序列化成功!");
      }catch(ClassNotFoundException e)  
      {  
           e.printStackTrace();
           System.out.println("反序列化失败!");
          } 
      catch (IOException e)  
      {  
       e.printStackTrace();  
      } 
}
    public static void main(String[] args){
        // TODO 自动生成的方法存根
        xuliehua n=new xuliehua();
        n.xulie();
           
    }

}

 

package open_exe;

import java.io.Serializable;

public class Student implements Serializable{//注意这儿需要实现serializable接口
    private static final long serialVersionUID = 7146811040856818256L;
    private String name;
    private String sex;
    private int age;
    private String height;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }
    

}

 

java 序列化与反序列化