1 import java.io.Serializable; 2 3 /** 4 * @author Administrator 5 * 用户信息封装 6 */ 7 public class User implements Serializable{ 8 private String name; 9 private String password; 10 public User() {} 11 public User(String name, String password) { 12 super(); 13 this.name = name; 14 this.password = password; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 public String getPassword() { 23 return password; 24 } 25 public void setPassword(String password) { 26 this.password = password; 27 } 28 }
. 1 Import java.io.File; 2 Import the java.io.Serializable; . 3 Import the java.util.HashMap; . 4 . 5 / ** . 6 * @author Administrator . 7 * Create Map collection, storing user information . 8 * / . 9 public class the UserInit the implements the Serializable { 10 static the HashMap <String, the user> users = new new the HashMap <> (); // Create a stored set of users . 11 static { 12 is ObjectOutInputStreamDome.oboi ( "Verde", "123456" ); 13 is } 14 }
1 import java.io.FileNotFoundException; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.ObjectOutputStream; 5 6 /** 7 * @author Administrator 8 * 序列化输出流 9 */ 10 public class ObjectOutInputStreamDome { 11 public static void oboi(String name,String password) { 12 FileOutputStream fo = null; 13 ObjectOutputStream oos = null; 14 15 try { 16 fo = new FileOutputStream("G:/垃圾/User.txt"); 17 oos = new ObjectOutputStream(fo); 18 User user = new User(name,password); 19 UserInit.users.put(name, user); 20 oos.writeObject(UserInit.users); 21 } catch (FileNotFoundException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 }finally { 28 try { 29 if(oos!=null) { 30 oos.flush(); 31 oos.close(); 32 } 33 if(fo!=null) { 34 fo.close(); 35 } 36 } catch (IOException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 } 41 } 42 }
1 /** 2 * @author Administrator 3 * 序列化输入流 4 */ 5 public class ObjextInputStreamDome { 6 public static HashMap<String,User> obin() { 7 FileInputStream fi = null; 8 ObjectInputStream ois = null; 9 10 try { 11 fi = new FileInputStream("G:/垃圾/User.txt"); 12 ois = new ObjectInputStream(fi); 13 HashMap<String, User> users = (HashMap<String, User>)ois.readObject(); 14 return users; 15 } catch (FileNotFoundException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } catch (ClassNotFoundException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 }catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } finally { 25 try { 26 ois.close(); 27 fi.close(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 return null; 34 } 35 }
. 1 / ** 2 * @author Administrator . 3 * sign-on operation . 4 * / . 5 public class UserLogin { . 6 // Log . 7 public static void the Login () { . 8 Boolean In Flag = to false ; . 9 do { 10 System.out.println ( " Please enter your login account: " ); 11 String name = TestDome.input.next (); 12 System.out.println (" Please enter your password: " ); 13 String password =TestDome.input.next (); 14 In Flag = ISEXIST (name, password); 15 IF (In Flag) { 16 return ; . 17 } the else { 18 is System.out.println ( "user name or password error does not exist, please re-enter ! " ); . 19 } 20 is } the while (! In Flag); 21 is 22 is 23 is } 24 25 // determine whether the correct 26 is public static Boolean ISEXIST (String name, String password) { 27 the HashMap <String, the User> Users = ObjextInputStreamDome.obin(); 28 Set<String> keys = users.keySet(); 29 for(String key:keys) { 30 User user = users.get(key); 31 if(user.getName().equals(name)&&user.getPassword().equals(password)) { 32 System.out.println("登录成功!"); 33 return true; 34 } 35 } 36 return false; 37 } 38 }
. 1 Import the java.util.HashMap; 2 Import java.util.Set; . 3 . 4 / ** . 5 * @author Administrator . 6 * registration operation . 7 * / . 8 public class userlogon { . 9 // Register 10 public static void Logon () { . 11 Boolean In Flag = to true ; 12 is do { 13 is System.out.println ( "Please enter the account name:" ); 14 String name = TestDome.input.next (); 15 System.out.println ( "Enter password:" ); 16 String password = TestDome.input.next (); 17 ObjectOutInputStreamDome.oboi (name, password); 18 Flag = ISEXIST (name, password); 19 IF (In Flag) { 20 is return ; 21 is } 22 is } the while (! In Flag); 23 is 24 } 25 26 is // determined that there is 27 public static Boolean ISEXIST (String name, String password) { 28 The HashMap <String, the User> Users = ObjextInputStreamDome.obin (); 29 the Set <String> Keys = users.keySet (); 30 for (String Key: Keys) { 31 is the User User = users.get (Key); 32 IF ( ! user.getName () the equals (name)) {. 33 is System.out.println ( "registered successfully!" ); 34 is ObjectOutInputStreamDome.oboi (name, password); 35 return to true ; 36 } 37 [ } 38 is the System.out. println ( "username already exists, please re-enter!" ); 39 return false; 40 } 41 }
1 /** 2 * @author Administrator 3 * 测试类 4 */ 5 public class TestDome { 6 static Scanner input = new Scanner(System.in); 7 public static void main(String[] args) { 8 System.out.println("1、登录\t2、注册"); 9 String choose = input.next(); 10 switch (choose) { 11 case "1": 12 UserLogin.Login(); 13 break; 14 case "2": 15 UserLogon.Logon(); 16 break; 17 default: 18 break; 19 } 20 } 21 }