如何将用户和密码输入与文本文件进行比较?
嘿!我正在使用Java中的Swing软件包创建一个简单的登录表单,并且在检查用户名和密码是否正确时遇到困难。
这是当前的代码:
public void actionPerformed(ActionEvent e) {
try{
File user = new File("Usernames.txt");
File pass = new File("Passwords.txt");
FileReader frUsername = new FileReader(user);
FileReader frPassword = new FileReader(pass);
BufferedReader brUsername = new BufferedReader(frUsername);
BufferedReader brPassword = new BufferedReader(frPassword);
String username = brUsername.readLine();
String password = brPassword.readLine();
if (e.getSource() == btnLogin){
while(username != null && password != null){
if ((txtUsername.getText()).equals(username) && (new String (jpfPassword.getPassword()).equals(password))){
JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful",JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
}
else{
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login",0); //this is for testing purposes only
}
break;
}
}
brUsername.close();
brPassword.close();
}
catch(IOException err){
System.err.println("File not found.");
}
}
}
这个想法是在密码和用户名文件中存储多个帐户。例如,文件内容为:
username.txt:
SampleUsername1
SampleUsername2
password.txt:
SamplePassword1
SamplePassword2
如果从用户名文件中的第1行是“ sampleusername1”,则其密码也来自密码文件的第1行“ samplepassword1”。如果用户和密码在文件中不相同,则应给出“无效”错误。我知道将密码放入TXT文件并不安全,但这仅是出于实践目的,因为我仍在学习如何编码。任何类型的帮助和技巧都非常感谢。谢谢!
Heyoo! I'm creating a simple login form using the swing package in java and I am having trouble in checking if the username and password is correct or not.
this here is the code currently:
public void actionPerformed(ActionEvent e) {
try{
File user = new File("Usernames.txt");
File pass = new File("Passwords.txt");
FileReader frUsername = new FileReader(user);
FileReader frPassword = new FileReader(pass);
BufferedReader brUsername = new BufferedReader(frUsername);
BufferedReader brPassword = new BufferedReader(frPassword);
String username = brUsername.readLine();
String password = brPassword.readLine();
if (e.getSource() == btnLogin){
while(username != null && password != null){
if ((txtUsername.getText()).equals(username) && (new String (jpfPassword.getPassword()).equals(password))){
JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful",JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
}
else{
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login",0); //this is for testing purposes only
}
break;
}
}
brUsername.close();
brPassword.close();
}
catch(IOException err){
System.err.println("File not found.");
}
}
}
The idea is to have multiple accounts stored in the password and usernames files. for example the file content is:
Username.txt:
SampleUsername1
SampleUsername2
Password.txt:
SamplePassword1
SamplePassword2
If line 1 from the username file is "sampleUsername1" then the password for it is also from line 1 "samplePassword1" of the password file. if the user and password isn't the same line or not in the file, it should give an "invalid" error. I know it is not secure to put passwords in a txt file but this is only for practice purposes as I am still learning how to code. Any kind of help and tips is really appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我来说很好:
我的
usernames.txt
和passwords.txt
看起来分别
是这样的主要问题是您只检查第一行。将
break;
更改为readline()
方法后,并且每次检查名称时都不会给出失败的消息。这就是为什么您必须首先循环遍历所有内容,然后检查是否失败。This works fine for me:
My
Usernames.txt
andPasswords.txt
look like thisrespectively
The main problem was that you were only checking the first line. Once you changed the
break;
to thereadline()
methods, the and you can't give the failed message everytime you check a name. That's why you have to loop through everything first, and then check if you failed or not.