如何将用户和密码输入与文本文件进行比较?

发布于 2025-01-29 00:43:20 字数 1685 浏览 2 评论 0原文

嘿!我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2025-02-05 00:43:20

这对我来说很好:

public static void main(String[] args) {
    String txtUsername = "username2";
    String jpfPassword = "password2";
    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();

        boolean loginSuccess = false;
        while (username != null && password != null) {

            if ((txtUsername).equals(username) && (jpfPassword.equals(password))) {
                JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful", JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
                loginSuccess = true;
                break;
            }
            username = brUsername.readLine();
            password = brPassword.readLine();
        }
        if (!loginSuccess) {
            JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login", 0); //this is for testing purposes only
        }
        brUsername.close();
        brPassword.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

我的usernames.txtpasswords.txt看起来

username
username1
username2

分别

password
password1
password2

是这样的主要问题是您只检查第一行。将break;更改为readline()方法后,并且每次检查名称时都不会给出失败的消息。这就是为什么您必须首先循环遍历所有内容,然后检查是否失败。

This works fine for me:

public static void main(String[] args) {
    String txtUsername = "username2";
    String jpfPassword = "password2";
    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();

        boolean loginSuccess = false;
        while (username != null && password != null) {

            if ((txtUsername).equals(username) && (jpfPassword.equals(password))) {
                JOptionPane.showMessageDialog(null, "Welcome: " + username, "Login Successful", JOptionPane.INFORMATION_MESSAGE); //this is for testing purposes only
                loginSuccess = true;
                break;
            }
            username = brUsername.readLine();
            password = brPassword.readLine();
        }
        if (!loginSuccess) {
            JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Unable to Login", 0); //this is for testing purposes only
        }
        brUsername.close();
        brPassword.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

My Usernames.txt and Passwords.txt look like this

username
username1
username2

respectively

password
password1
password2

The main problem was that you were only checking the first line. Once you changed the break; to the readline() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文