把你注册的用户名和密码用配置文件保存,登录时从配置文件读取用户名和密码
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//把你注册的用户名和密码用配置文件保存,登录时从配置文件读取用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("注册账号:");
System.out.println("请输入你的账号");
String username = sc.nextLine();
System.out.println("请输入你的密码");
String password = sc.nextLine();
Account ac = getAccount(username, password);
write(ac);
read();
}
private static void read() throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("account.properties"));
Object a = in.readObject();
Account account = (Account) a;
System.out.println(account.getUsername() + "=" + account.password);
}
private static Account getAccount(String username, String password) {
return new Account(username, password);
}
private static void write(Account ac) throws IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("account.properties"));
out.writeObject(ac);
}
}
import java.io.Serializable;
public class Account implements Serializable {
private static final long serialVersionUID = 8514220324172857230L;
String username;
String password;
public Account(String username, String password) {
this.username = username;
this.password = password;
}
public Account() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}