Java --> IO stream simulation for user login and login information, java -- io
--> Test class
Package com. dragon. java. hwlogin; import java. io. fileNotFoundException; import java. util. hashSet; import java. util. imports; import java. util. set;/** simulate user login and record login logs. A: Select whether to register a user or log on to the user. B: If user registration saves the user name and password registered by the user in the file, the file can record multiple user names and passwords. C: each user logs on to the system every time, a login record is generated, and the login record is saved in a file to form a login log. Login record format (username, password, Logon Time): = zhangsan, xxxxx, August 16, 2016 13:20:20 = d: If you log on as the Admin administrator, you can view logon records of all users. Otherwise, other users can only view their login records. */Public class Test {static Set <User> set = new HashSet <> (); @ SuppressWarnings ("resource") public static void main (String [] args) throws FileNotFoundException {writable partition = new partition (System. in); AdminUser adminUser = new AdminUser (); User thisUser = null; // first, save the previous User information to adminUser in the collection. splitFile (AdminUser. filePath2, set); while (true) {System. out. println ("----- welcome to xxx System -----"); System. out. Println ("1. registered User 2. login User "); System. out. println ("3. log on as an administrator. 4. exit "); System. out. println ("Enter the serial number:"); String name = null, password = null; switch (response. nextInt () {case 1: System. out. println ("----- User Registration: -----"); System. out. println ("Enter the User name:"); name = login. next (); System. out. println ("enter the password:"); password = login. next (); System. out. println ("Confirm Password:"); if (! Passwords. next (). equals (password) {System. out. println ("the two passwords are different! "); Continue;} if (set. size ()! = 0) {if (adminUser. userExist (name, set )! = Null) {System. out. println ("this user already exists! "); Break;} else {adminUser. userRegister (name, password, set) ;}} else {adminUser. userRegister (name, password, set);} break; case 2: System. out. println ("----- User Logon: -----"); if (set. size ()! = 0) {System. out. println ("Enter the User name:"); name = login. next (); System. out. println ("enter the password:"); password = login. next (); if (thisUser = adminUser. userExist (name, set) = null) {System. out. println ("this user does not exist! ");} Else if (! Password. equals (thisUser. getPassword () {System. out. println ("Incorrect password! ");} Else {System. out. println ("dear" + thisUser. getName () + ", your logon record is :"); // obtain the logon information of all users and compare it with the current User. Set <User> setTime = new HashSet <> (); adminUser. splitFile (AdminUser. filePath, setTime); for (User user: setTime) {if (thisUser. getName (). equals (user. getName () {System. out. println (user) ;}} System. out. println ("You are now" + adminUser. loginTime () + "Log on! "); AdminUser. userLogin (name, password, adminUser. loginTime (), set) ;}} else {System. out. println (" no user registration yet! "); Continue;} break; case 3: adminUser. readUser (); break; case 4: System. out. println (" exited! "); System. exit (0); default: System. out. println (" incorrect input !... "); Continue ;}}}}
--> The AdminUser class controls user logon and enters information
Package com. dragon. java. hwlogin; import java. io. bufferedReader; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. fileReader; import java. io. IOException; import java. io. inputStream; import java. io. printStream; import java. text. simpleDateFormat; import java. util. date; import java. util. locale; import java. util. set; class AdminUser {@ SuppressWarnings ("unused") private static I NputStream in = System. in; private static PrintStream out = System. out; private static PrintStream ps = null; private static BufferedReader br = null; static String filePath = "D: /workspace/08-16/src/com/dragon/java/hwlogin/login.txt "; static String filePath2 =" D: /workspace/08-16/src/com/dragon/java/hwlogin/users.txt "; /*** determine whether a user exists ** @ param name * @ param set * @ return */public User userEx Ist (String name, Set <User> set) {for (User user: set) {if (name. equals (user. getName () {return user;} return null ;} /*** User Logon ** @ param name * @ param password * @ param time * @ param set */public void userLogin (String name, String password, String time, set <User> set) {User user = new User (name, password, time); writeUser (ps, user, filePath, set );} /***** user registration ** @ param name * @ param Password * @ param set */public void userRegister (String name, String password, Set <User> set) {User user = new User (name, password); writeUser (ps, user, filePath2, set);}/*** previous logon information for logon, read the current system time ** @ return */public String loginTime () {SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("MM dd, yyyy hh: mm: ss", Locale. CHINA); Date date = new Date (System. currentTimeMillis (); String t Ime = simpleDateFormat. format (date); // return time of the current system time;}/*** read logon information of all users */public void readUser () {try {br = new BufferedReader (new FileReader (filePath); String line = null; while (line = br. readLine ())! = Null) {System. out. println (line) ;}} catch (IOException e) {System. out. println (e);} finally {try {br. close ();} catch (IOException e) {System. out. println (e) ;}}/ *** sets user information (or logon information) write ** @ param ps * @ param user * @ param file * @ param set */public void writeUser (PrintStream ps, User user, String file, set <User> set) {set. add (user); try {// FileOutputStream (file, true) --> true indicates confirming file Add --> that is, do not refresh the file ps = new PrintStream (new FileOutputStream (file, true), true); System. setOut (ps); System. out. println (user);} catch (FileNotFoundException e) {System. out. println (e);} finally {// reset the "standard" output to the console System. setOut (out );}} /*** split the file storing information to get User data and save it to the collection ** @ param file * @ param set */public void splitFile (String file, Set <User> set) {try {br = new BufferedReader (new FileReader (file); S Tring line = null; while (line = br. readLine ())! = Null) {String [] strings = line. split (","); User user = new User (strings [0]. substring (11, strings [0]. length (), strings [1]. substring (10, strings [1]. length (), strings [2]. substring (6, strings [2]. length (); set. add (user) ;}} catch (IOException e) {System. out. println (e);} finally {try {br. close ();} catch (IOException e) {System. out. println (e );}}}}
--> User class stores User attributes
package com.dragon.java.hwlogin;class User { private String name; private String password; private String time; User() { super(); } User(String name, String password) { super(); this.name = name; this.password = password; } User(String name, String password, String time) { this(name, password); this.time = time; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } @Override public String toString() { return "User [name=" + name + ", password=" + password + ", time=" + time + "]"; }}
--> Before redirecting an input/output stream, you must first save the default System input/output stream (namely, System. in And System. out) to restore...