一个小的登录程序,一个系统中的用户通常有若干个,那.properties文件中的username和password应该怎么写?
我这样写:
username=lily
password=123
username=lucy
password=456但这样就使得第一个username和password无效了。
请问该怎么办
我这样写:
username=lily
password=123
username=lucy
password=456但这样就使得第一个username和password无效了。
请问该怎么办
import java.io.*;public class UserLogin { Properties prop;
public UserLogin() throws Exception {
prop = new Properties();
FileInputStream file = new FileInputStream("sample.properties");
prop.load(file);
}
public UserLogin(String propPath) throws Exception {
prop = new Properties();
FileInputStream file = new FileInputStream(propPath);
prop.load(file);
}
public String getUsername() {
return prop.getProperty("username");
}
public String getPassword() {
return prop.getProperty("password");
}
public static void main(String[] a) throws Exception {
UserLogin user = new UserLogin();
Scanner s = new Scanner(System.in);
do {
System.out.println("请输入用户名:");
String username = s.next().trim();
System.out.println("请输入密码:");
String pwd = s.next().trim();
if(username.equals(user.getUsername())&&
pwd.equals(user.getPassword()))
{
System.out.println("欢迎进入收银系统!");
break;
}
else
{
System.out.println("您输入的用户名或密码错误!");
System.out.println("是否继续登录?[y:重新输入][任意键:退出登录]");
String op = s.next().trim().toLowerCase();
if(!op.equals("y"))
{
System.out.println("退出登录系统!");
break;
}
}
}while(true);
}
}