将对象写入文本文档、从文本文档中读取对象

版权声明:原创内容是本人学习总结,仅限学习使用,禁止用于其他用途。如有错误和不足,欢迎评论指正补充。 https://blog.csdn.net/qian_qian_123/article/details/85238902

将对象写入文本文档: 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
 
/**
 * 对象IO
 * 对象流,写入对象
 * @author wendi
 *
 */
public class Demo11 {
    public static void main(String[] args) {
        //创建一个学生
        Student stu = new Student(1001,"张三",21);
        //把这个学生保存到  d:/info/stu.txt   ObjectOutputStream
        ObjectOutputStream  oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("d:/info/stu.txt"));
            //往文件中写入student对象
            oos.writeObject(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(oos != null){
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
}

从文本文档中读取对象

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
 
/**
 * 使用ObjectInputStream 反序列化
 * 对象流,读取对象
 * @author wendi
 *
 */
public class Demo12 {
    public static void main(String[] args) {
        ObjectInputStream ois = null;
        try {
            ois = new  ObjectInputStream(new FileInputStream("d:/info/stu.txt"));
            //读取对象  Object readobject()
           Student stu =  (Student) ois.readObject();
           System.out.println(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            try {
                if(ois != null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

用到的实体类:

import java.io.Serializable;
 
/**
 * 
 * @author wendi
 *
 */
public class Student implements Serializable {
    /**
     * 随机产生一个序列化ID,  序列化:  stu.txt保存学生的所有的信息, 保存 序列化ID写到文件中
     *   反序列化: 从stu.txt读取文件, 二进制文件, 完整的Student对象
     */
    private static final long serialVersionUID = 8402631090764643122L;
    /**
     *  固定的序列化ID     1L
     */
    //private static final long serialVersionUID = 1L;
    private  int id;
    private String name;
    private transient int age;//利用关键字transient隐藏age信息。
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Student() {
    }
    
    
    
}

猜你喜欢

转载自blog.csdn.net/qian_qian_123/article/details/85238902