序列化和反序列化 注册保留用户信息(IO流)

编写

 

package com.I_O;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

public class Object_IO { 
    /**
     *     登录main方法*/
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("1、注册\t2、登录\t3、退出");
        String choose=sc.next();
        switch (choose) {
        case "1":
            System.out.println("***注册***");
            System.out.println("注册的用户名");
            String name=sc.next();
            System.out.println("注册的账号");
            int idcard=sc.nextInt();
            System.out.println("注册的密码");
            String password=sc.next();
            User u=new User(idcard,password,name);    //创建用户
            IO.output(u);                            //导出对象
        case "2":
            System.out.println("***登录*** ");
            System.out.println("请输入账号");
            idcard=sc.nextInt();
            System.out.println("请输入密码");
            password=sc.next();
            u=new User(idcard,password,null);        //创建对象
            u=IO.input(u);
            if(u==null) {
                System.out.println("***登录失败***");
            }else {
                System.out.println("***登录成功***");
            }
            break;
        case "3":
            System.out.println("谢谢使用");
            break;
        }
    }
}

/**
 *     用户类
 */
class User implements Serializable{
    /**
     *     用户属性
     */
    int idcard;            //账号
    String password;    //密码
    String name;        //用户名
    
    /**
     *     构造方法
     */
    public User () {}
    public User(int idcard, String password, String name) {
        super();
        this.idcard = idcard;
        this.password = password;
        this.name = name;
    }
}
/**
 *     输入输出对象
 */
class IO{
    /**
     *     输入对象    反序列化
     */
    public static User input(User u) {
        ObjectInputStream ois=null;
        try {
            ois=new ObjectInputStream(new FileInputStream(u.idcard+".txt"));
            User u1=(User)ois.readObject();
            if(u1.idcard==u.idcard&&u1.password.equals(u.password)) {
                return u1;
            }
        } catch (IOException e) {
//            e.printStackTrace();
        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
        }finally {
            try {
                if(ois!=null)
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     *     输出对象    序列化
     */
    public static void output(User u) {
        ObjectOutputStream ois=null;
        try {
            ois=new ObjectOutputStream(new FileOutputStream(u.idcard+".txt"));
            ois.writeObject(u);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }
}

 

 

 

运行

 

重新运行

 

再次运行

 

转载于:https://www.cnblogs.com/zzh630/p/10497991.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值