实现用户登录:键盘输入要登录的用户名与密码
properties类型文件常在框架内用作配置文件.
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("user.properties");
Properties properties = new Properties();
properties.load(fis);
Set<String> strings = properties.stringPropertyNames();
System.*out*.println(strings);
Scanner scanner = new Scanner(System.*in*);
System.*out*.println("请输入用户名");
String userName = scanner.next();
if (properties.getProperty(userName) == null) {
System.*out*.println("用户未注册,请先注册!");
System.*exit*(0);
}
System.*out*.println("请输入密码");
int i = 0;
do {
String passWord = scanner.next();
if (properties.getProperty(userName).equals(passWord)) {
System.*out*.println("成功登录");
break;
} else {
if (2 - i == 0) {
System.*out*.println("忘记密码?请按要求找回密码");
} else {
System.*out*.println("密码错误,请重新输入,还有" + (2 - i) + "机会");
}
}
} while (++i < 3);
fis.close();
}
文件内容如下