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

问题描述:

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

我的文本文件看起来是这样的:

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

READFILE:

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; 
} 
} 

使用一个int count用于在超过尝试显示消息并终止程序之后存储尝试米

+0

什么是menu = new Mainemenu(); menu.AdminMenu(); ?? –

+0

只是重定向你想要的地方 –