模拟用户登录功能实现。接收用户输入的用户名和密码,然后和文件中存储的用户名、密码匹配
config.txt,中存的内容如下:
userName=admin
passWord=123456
public static void main(String[] args) throws IOException {
String[] arr = new String[2];
BufferedReader br = new BufferedReader(new FileReader("config.txt"));
String[] arr1 = br.readLine().split("=");
String[] arr2 = br.readLine().split("=");
br.close();
User u0 = new User(arr1[1], arr2[1]);
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入用户名:");
String userName = sc.nextLine();
System.out.println("请输入密码:");
String passWord = sc.nextLine();
User u = new User(userName,passWord);
if(u.equals(u0)){
System.out.println("登陆成功");
break;
}else{
System.out.println("用户名或密码不正确,请重新登陆:");
}
}
}
} |
|