zoukankan      html  css  js  c++  java
  • Java(31):序列化和反序列化

    package zzz;
    
    import java.io.File;
    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;
    
    public class SerializationTest {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException {
    
            Student stu = new Student("zzz", "123456", 18);
    
    // 序列化:把对象信息写入到本地文件里
    // 1.File对象打开本地文件
            File file = new File("f:\object.ser");
    // 2.建立数据通道
            FileOutputStream fos = new FileOutputStream(file);
    // 3.创建输出流对象:把对象转成字节数据的输出到文件中保存
    // 构造方法中传递字节输出流
            ObjectOutputStream ops = new ObjectOutputStream(fos);
    // 4.对象写入到硬盘上
            ops.writeObject(stu);
    // 5.释放资源
            ops.close();
    
            System.out.println(stu);
    
    // 反序列化
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Student student = (Student) ois.readObject();
            System.out.println(student);
            System.out.println(student.getUserName() + ", " + student.getPassWord() + ", " + student.getAge());
        }
    
    }
    
    class Student implements Serializable {
        public static final long UID = 123L;
    
        private String userName;
        private String passWord;
        private int age;
    
        public Student(String userName, String passWord, int age) {
            this.userName = userName;
            this.passWord = passWord;
            this.age = age;
            System.out.println("Student end.");
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserName() {
            return this.userName;
        }
    
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
    
        public String getPassWord() {
            return this.passWord;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getAge() {
            return this.age;
        }
    
        @Override
        public String toString() {
            return "Student: userName=" + userName + ", passWord=" + passWord + ", age=" + age;
        }
    
    }
  • 相关阅读:
    C. Shaass and Lights 解析(思維、組合)
    D. Binary String To Subsequences(队列)(贪心)
    CodeForces 1384B2. Koa and the Beach (Hard Version)(贪心)
    CodeForces 1384B1. Koa and the Beach (Easy Version)(搜索)
    CodeForces 1384C. String Transformation 1(贪心)(并查集)
    CodeForces 1384A. Common Prefixes
    POJ-2516 Minimum Cost(最小费用最大流)
    POJ3261-Milk Patterns(后缀数组)
    HDU-1300 Pearls(斜率DP)
    HDU-4528 小明系列故事-捉迷藏(BFS)
  • 原文地址:https://www.cnblogs.com/kenantongxue/p/13984499.html
Copyright © 2011-2022 走看看