zoukankan      html  css  js  c++  java
  • 39.使用序列化和反序列化

    public class Student implements java.io.Serializable  {    
        private String name;
        private int age;
        private String gender;
        private transient String password;
        public Student(String name, int age,String gender){
            this.name=name;
            this.age=age;
            this.gender=gender;   
        }
        
        public Student(String name, int age,String gender,String password){
            this.name=name;
            this.age=age;
            this.gender=gender; 
            this.password=password;
        }
        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;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    }
    import java.io.*;
    
    public class SerializableObj {
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            ObjectOutputStream oos = null;
            ObjectInputStream ois=null;
            try {
                // 创建ObjectOutputStream输出流
                oos = new ObjectOutputStream(new FileOutputStream(
                        "c:\myDoc\stu.txt"));
                Student stu = new Student("安娜", 30, "女","aaaa");
                System.out.println("姓名为:"+stu.getName());
                System.out.println("年龄为:"+stu.getAge());
                System.out.println("性别为:"+stu.getGender());
                System.out.println("密码为:"+stu.getPassword());
                // 对象序列化,写入输出流
                oos.writeObject(stu);
                
                //创建ObjectInputStream输入流
                ois=new ObjectInputStream(new FileInputStream("c:\myDoc\stu.txt"));
                //反序列化,强转类型
                Student stu1=(Student)ois.readObject();
                //输出生成后对象信息
                System.out.println("姓名为:"+stu1.getName());
                System.out.println("年龄为:"+stu1.getAge());
                System.out.println("性别为:"+stu1.getGender());
                System.out.println("密码为:"+stu1.getPassword());
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (oos != null) {
                        oos.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                }catch (IOException ex) {
                    ex.printStackTrace();
                }                     
            }
        }
    }
    public class Test {
        
        private String name;
        public Test(){
            this.name="小新";
        }
        public Test(String name){
            System.out.println("我是:"+this.name);
        }
        public static void main(String[] args) {
            Test test=new Test("小强");
        }
    }
  • 相关阅读:
    新家开始了
    FreeMarker过时的方法 new Configuration()
    基于Spring封装的Javamail实现邮件发送
    Spring中PropertiesLoaderUtils应用
    SpringBoot基于数据库的定时任务统一管理
    ActiveMQ点对点模式
    springboot整合swagger2
    dubbo简单示例
    SpringCLoud之搭建Zuul网关集群
    微服务项目框架搭建
  • 原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536579.html
Copyright © 2011-2022 走看看