![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package lianxi802; import java.io.Serializable; public class User implements Serializable { private String name; private int age; private String sex; private String code; @Override public String toString() { return "User [name=" + name + ", age=" + age + ", sex=" + sex + ", code=" + code + "]"; } public User() { super(); } public User(String name, int age, String sex, String code) { super(); this.name = name; this.age = age; this.sex = sex; this.code = code; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package lianxi802; 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.util.*; public class UserSignIn { static Scanner input = new Scanner(System.in); // 存储注册的用户 static Map<String, User> map = new HashMap<String, User>(); // 注册10个用户 static { User u1 = new User("zt1", 20, "男", "123456"); User u2 = new User("zt2", 21, "男", "123456"); User u3 = new User("zt3", 22, "男", "123456"); User u4 = new User("zt4", 23, "男", "123456"); User u5 = new User("zt5", 24, "男", "123456"); User u6 = new User("zt6", 25, "男", "123456"); User u7 = new User("zt7", 26, "男", "123456"); User u8 = new User("zt8", 27, "男", "123456"); User u9 = new User("zt9", 28, "男", "123456"); User u10 = new User("zt10",29, "男", "123456"); map.put(u1.getName(), u1); map.put(u2.getName(), u2); map.put(u3.getName(), u3); map.put(u4.getName(), u4); map.put(u5.getName(), u5); map.put(u6.getName(), u6); map.put(u7.getName(), u7); map.put(u8.getName(), u8); map.put(u9.getName(), u9); map.put(u10.getName(), u10); } //存储注册的用户 public static void cc(User user) { map.put(user.getName(), user); xlh(); } // 序列化集合 public static void xlh() { ObjectOutputStream oos = null; FileOutputStream fos = null; try { fos = new FileOutputStream("g:/users"); oos = new ObjectOutputStream(fos); oos.writeObject(map); oos.flush(); // System.out.println("已完成序列化集合!"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { oos.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 用户登入 public void signIn() { System.out.println("……………………………………………………登入……………………………………………………"); System.out.println("请输入用户名:"); String name = input.next(); System.out.println("请输入用户密码:"); String code = input.next(); // 反序列化集合 FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream("g:/users"); ois = new ObjectInputStream(fis); Map<String, User> map = (Map<String, User>) ois.readObject(); // 验证 if (map.get(name) != null) { // 验证密码 if (code.equals(map.get(name).getCode())) { System.out.println("登入成功,您的账号信息如下:"); System.out.println(map.get(name)); } else { System.out.println("密码输入错误,登入失败!"); } } else { System.out.println("登入失败,没有该用户名!"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { ois.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package lianxi802; import java.util.*; //用户注册 public class UserZC { static Scanner input = new Scanner(System.in); public void zc() { System.out.println("请输入用户名:"); String name = input.next(); System.out.println("请输入您的年龄:"); int age = input.nextInt(); System.out.println("请输入您的性别:"); String sex = input.next(); System.out.println("请输入用户密码:"); String code = input.next(); User u = new User(name, age, sex, code); UserSignIn.map.put(name, u); System.out.println("注册成功!"); UserSignIn.cc(u); } }
运行
先注册再登入
直接登入