• 序列化与反序列化 ---密码


    package xuliehua;
    
    import java.io.Serializable;
    
    //登录Enter 注册Enroll属性
    public class EnrollAndEnter implements Serializable{
        private String name;
        private int vip;
        private String possword;
        
        
        /**
         * 无参构造方法
         */
        public EnrollAndEnter() {
        }
        
        
    
        /**
         * 有参构造方法
         * @param name
         * @param vip
         * @param possword
         */
        
        public EnrollAndEnter(String name,String possword) {
            this.name = name;
            this.possword = possword;
        }
    
    
    
        public String getName() {
            return name;
        }
    
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getVip() {
            return vip;
        }
    
        public void setVip(int vip) {
            this.vip = vip;
        }
    
        public String getPossword() {
            return possword;
        }
    
        public void setPossword(String possword) {
            this.possword = possword;
        }
        
        
    }
    
    
    
    package xuliehua;
    
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    //创建注册的方法并序列化
    public class ObjectIO {
        static Scanner input = new Scanner(System.in);
        EnrollAndEnter ele = new EnrollAndEnter();
    
    
        public void bigen() {
            do {
                System.out.println("请选择:1、注册    2、登录     3、退出");
                String str = input.next();
                switch(str) {
                case "1":
                    //创建对象
    
                    System.out.println("请输入账户名:");
                    ele.setName(input.next());
                    System.out.println("请输入密码:");
                    ele.setPossword(input.next());
                    System.out.println("注册成功");
    
                    ObjectOutputStream oos = null;
                    FileOutputStream fos = null;
                    try {
                        //创建序列化对象
                        System.out.println(ele.getName());
                        fos = new FileOutputStream("d:/"+ele.getName()+".txt");
                        oos = new ObjectOutputStream(fos);
                        oos.writeObject(ele);
                        oos.flush();
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            oos.flush();
                            oos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case "2":
    
                    //创建反序列化对象
                    ObjectInputStream ois = null;
    
                    try {
    
                        System.out.println("您的账户名是:"+ele.getName()+",你的密码是:"+ele.getPossword());
                        System.out.println("请输入账户名:");
                        ele.setName(input.next());
                        System.out.println("请输入密码:");
                        ele.setPossword(input.next());
                        //反序列化
                        ois = new ObjectInputStream(new FileInputStream("d:/"+ele.getName()+".txt"));
                        EnrollAndEnter eae = (EnrollAndEnter)ois.readObject();
                        if(ele.getName().equals(eae.getName()) && ele.getPossword().equals(eae.getPossword())) {
                            System.out.println("登录成功");
                        }else {
                            System.out.println("账户名或者密码错误!");
                        }
                    } catch (IOException e) {
    //                    e.printStackTrace();
                        System.out.println("账户名或者密码错误!");
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if(ois!=null) {
                                ois.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case "3":
                    System.out.println("谢谢使用");
                    return;
                default :
                    System.out.println("输入有误请重输:");
                }
    
                
    
    
            }while(true);
        }
    }
    
    
    
    
    package xuliehua;
    //测试序列化
    import java.util.Scanner;
    
    public class ObjectIOText {
        public static void main(String[] args) {
    
            ObjectIO ot = new ObjectIO();
            ot.bigen();
        }    
    }

  • 相关阅读:
    hdu7047 /2021“MINIEYE杯”中国大学生算法设计超级联赛(7) 1004 Link with Balls
    hdu7115 Vertex Deletion
    CF1552B B. Running for Gold
    hdu7055 /2021“MINIEYE杯”中国大学生算法设计超级联赛(7) 1012 Yiwen with Sqc
    hdu7050 /2021“MINIEYE杯”中国大学生算法设计超级联赛(7) 1007 Link with Limit
    CF 1560E E. Polycarp and String Transformation(思维)
    CF 1542C C. Strange Function
    STL 算法 std::distance
    STL 算法 std::accumulate
    std::get<C++11多线程库~线程管理>(08):转移线程所有权(2)
  • 原文地址:https://www.cnblogs.com/zxbaoer/p/10500655.html
Copyright © 2020-2023  润新知