如何在java中使用用户名和密码从一个包含10条记录的文本文件中读取来登录?

如何在java中使用用户名和密码从一个包含10条记录的文本文件中读取来登录?,java,tokenize,Java,Tokenize,该消息还应显示用户尝试登录的次数。如果用户达到最大尝试次数,程序将被终止,并显示“超出尝试次数。程序终止”的消息对话框。 如果文本文件中只有一个密码和一个用户名,“我的代码”接受用户名和密码。如何扫描文本文件中的所有10个数据,如果它与用户的输入匹配,它将授予访问权限 我的文本文件如下所示: [awe1,pass1] [awe2,pass2] [awe3,pass3] [awe4,pass4] [awe5,pass5] [awe6,pass6]

该消息还应显示用户尝试登录的次数。如果用户达到最大尝试次数,程序将被终止,并显示“超出尝试次数。程序终止”的消息对话框。 如果文本文件中只有一个密码和一个用户名,“我的代码”接受用户名和密码。如何扫描文本文件中的所有10个数据,如果它与用户的输入匹配,它将授予访问权限

我的文本文件如下所示:

    [awe1,pass1]
    [awe2,pass2]
    [awe3,pass3]
    [awe4,pass4]
    [awe5,pass5]
    [awe6,pass6]
    [awe7,pass7]
    [awe8,pass8]
    [awe9,pass9]
    [awe10,pass10]
读取文件:

private static  String ReadFile(){
        String line=null;
        String text="";
        try{

            FileReader filereader=new FileReader(new File("MyLoginData.txt"));
             //FileReader filereader=new FileReader(new File(path));
            BufferedReader bf=new BufferedReader(filereader);
            while((line=bf.readLine()) !=null){
                text=text+line;

            }
            bf.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return text;

    }


   public void PassWordFrame()
   {

      btnOk.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                String info = ReadFile();
                System.out.println(info);
                String[] split = info.split(",");
                String uname=split[0];
                String pass =split[1];

                if(txtUsername.getText().equals(uname) && txtPassword.getText().equals(pass)){
                     JOptionPane.showMessageDialog (null, "Access granted", "Status", JOptionPane.INFORMATION_MESSAGE);
                }else{
                        JOptionPane.showMessageDialog (null, "Access Denied", "Status", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }); 

   }

简单的方法,你们可以在那个时读取文件,你们可以检查用户名和密码

while ((line= br.readLine()) !=null) {

// Split line by a whitespace character
// split[0] <- username
// split[1] <- password

line = line.replace("[","").replace("]","");
String[] split =line .split(",");

if (user.equals(split[0]) && pass.equals(split[1])) {

    //redirect where you want
    // You found the user, exit the loop
    break;
 }
}
while((line=br.readLine())!=null){
//用空格字符分隔行

//拆分[0]一种简单的方法,您可以在读取文件时检查用户名和密码

while ((line= br.readLine()) !=null) {

// Split line by a whitespace character
// split[0] <- username
// split[1] <- password

line = line.replace("[","").replace("]","");
String[] split =line .split(",");

if (user.equals(split[0]) && pass.equals(split[1])) {

    //redirect where you want
    // You found the user, exit the loop
    break;
 }
}
while((line=br.readLine())!=null){
//用空格字符分隔行

//拆分[0]what is menu=new mainemu();menu.AdminMenu();?只需重定向您想要的位置what is menu=new mainemu();menu.AdminMenu();?只需重定向您想要的位置