简体   繁体   English

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

[英]How can I compare User and password input to a text file?

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.我正在使用 java 中的 swing 包创建一个简单的登录表单,但在检查用户名和密码是否正确时遇到了麻烦。

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:用户名.txt:

       SampleUsername1
       SampleUsername2

Password.txt:密码.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.如果用户名文件的第 1 行是“sampleUsername1”,那么它的密码也来自密码文件的第 1 行“samplePassword1”。 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.我知道将密码放在 txt 文件中并不安全,但这仅用于练习目的,因为我仍在学习如何编码。 Any kind of help and tips is really appreciated.非常感谢任何形式的帮助和提示。 Thanks!谢谢!

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我的Usernames.txtPasswords.txt看起来像这样

username
username1
username2

respectively分别

password
password1
password2

The main problem was that you were only checking the first line.主要问题是您只检查第一行。 Once you changed the break;一旦你改变了break; to the readline() methods, the and you can't give the failed message everytime you check a name.对于readline()方法,每次检查名称时都不能给出失败的消息。 That's why you have to loop through everything first, and then check if you failed or not.这就是为什么您必须先遍历所有内容,然后检查您是否失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何将用户输入与文本文件中的列进行比较? - Java: How do I compare user input to columns in text file? 如何读取 csv 文本文件,以便将 csv 文件中的特定值与用户输入进行比较? - How do I read csv text file so I can compare specific values from csv file with user input? 如何将文本字段和密码字段与字符串值进行比较? - How can I compare text field and password field to String values? 如果文件名已经存在,如何让用户再次输入文本文件名? - How can I let user input the name of text file again if the name of file already exists? 如何读取和比较文本文件中的数字? - How can I read and compare numbers from a text file? 将用户输入打印到文本文件时如何插入换行符? - How can I insert line breaks when printing user input to a text file? 读取文本文件的每一行并将其与用户输入进行比较 - Reading each line of a text file and compare it to user input 如何比较用户名密码和密码与文本文件登录Java - How to compare username password and secret code with text file to login java 我可以屏蔽/加密 windows run.bat 文件中的输入文本(例如密码)吗? - Can i mask/encrypt input text (for ex password) in the windows run.bat file? 如何将用户输入从 JTextfield 添加到文本文件 - How do I add user input from JTextfield to Text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM