java对象序列化

/**
 * @version
 * @description
 */
package cn.xasmall.example;

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

/**
 * 对象与序列化
 * @author 26248
 *
 */
public class Serialization {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("data.txt"));
        Student stu1=new Student(23, "张三", "jifan", 334.2d);
        out.writeObject(stu1);
        out.close();
        ObjectInputStream in=new ObjectInputStream(new FileInputStream("data.txt"));
        Object student1=in.readObject();
        in.close();
        System.out.println(student1);
    }
}
//implements Externalizable继承后序列化机制失效
class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    //transient使序列化失效
    transient private int student_id;
    private String student_name;
    private String student_psw;
    private double student_money;
    /**
     * @return the student_id
     */
    public int getStudent_id() {
        return student_id;
    }
    /**
     * @param student_id the student_id to set
     */
    public void setStudent_id(int student_id) {
        this.student_id = student_id;
    }
    /**
     * @return the student_name
     */
    public String getStudent_name() {
        return student_name;
    }
    /**
     * @param student_name the student_name to set
     */
    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }
    /**
     * @return the student_psw
     */
    public String getStudent_psw() {
        return student_psw;
    }
    /**
     * @param student_psw the student_psw to set
     */
    public void setStudent_psw(String student_psw) {
        this.student_psw = student_psw;
    }
    /**
     * @return the student_money
     */
    public double getStudent_money() {
        return student_money;
    }
    /**
     * @param student_money the student_money to set
     */
    public void setStudent_money(double student_money) {
        this.student_money = student_money;
    }
    public Student(int id,String name,String psw,double money) {
        this.student_id=id;
        this.student_name=name;
        this.student_psw=psw;
        this.student_money=money;
    }
    public String toString() {
        return "student=["+student_id+","+student_name+","+student_psw+","+student_money+"]";
    }
//  writeObject()与readObject()两个方法使student_id再次被序列化
    private void writeObject(ObjectOutputStream out) throws IOException {
         out.defaultWriteObject(); 
         out.writeInt(student_id);;
    }
    private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
        in.defaultReadObject();
        student_id=in.readInt();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值