2014-01-06 53 views
2

在Java中,我希望能够拥有两个接受String的JTextField。一个是用户名,另一个是密码。为了“登录”,程序将搜索一个文件 - > accounts.txt,并首先搜索“usrnm:”,一旦找到它,它就会在 之后立即找到该密码,但它却是一样的搜索“pswrd:”。如何从输入文件读取指定的字符串,然后读取它后面的下一个字

这里是iv'e走到这一步:

public void checkCredentials() { 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(new File("accounts.txt"))); 
     String text = br.readLine(); 
     text = text.toLowerCase(); 
     text.split("usrnm:"); 
     System.out.println("username: " + userInput); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

任何帮助表示赞赏!

+2

你的问题是什么? –

+0

如果我的密码是“usrnm:”,该怎么办? – Catchwa

回答

3

而不是两次搜索文件我相信它更好地保存第一个用户名和下一行密码。读取它们时,您可以创建具有以下属性的用户对象(用户名和密码(字符串))。您也可以在系统处于活动状态时将其保存在链接列表中。以便您可以轻松访问用户帐户。一旦用户需要登录,您将扫描该列表并使用userList.get(i).equals(textfield.getText())来确定用户提问。之后,如果上述语句成立,您将检查userList.get(i).getPassword()。equals(textfield2.getText())是否为true,并相应地授予或拒绝访问。

我在下面提供了一些有用的部分。

public void readFromFile(String fileName, ListInterface<User> userList) { 
     String oneLine, oneLine2; 
     User user; 
     try { 
      /* 
      * Create a FileWriter object that handles the low-level details of 
      * reading 
      */ 
      FileReader theFile = new FileReader(fileName); 

      /* 
      * Create a BufferedReader object to wrap around the FileWriter 
      * object 
      */ 
      /* This allows the use of high-level methods like readline */ 
      BufferedReader fileIn = new BufferedReader(theFile); 

      /* Read the first line of the file */ 
      oneLine = fileIn.readLine(); 
      /* 
      * Read the rest of the lines of the file and output them on the 
      * screen 
      */ 
      while (oneLine != null) /* A null string indicates the end of file */ 
      { 
       oneLine2 = fileIn.readLine(); 
       user = new User(oneLine, oneLine2); 
       oneLine = fileIn.readLine(); 
       userList.append(user); 
      } 

      /* Close the file so that it is no longer accessible to the program */ 
      fileIn.close(); 
     } 

     /* 
     * Handle the exception thrown by the FileReader constructor if file is 
     * not found 
     */ 
     catch (FileNotFoundException e) { 
      System.out.println("Unable to locate the file: " + fileName); 
     } 

     /* Handle the exception thrown by the FileReader methods */ 
     catch (IOException e) { 
      System.out.println("There was a problem reading the file: " 
        + fileName); 
     } 
    } /* End of method readFromFile */ 


    public void writeToFile(String fileName, ListInterface<User> userList) { 
     try { 
      /* 
      * Create a FileWriter object that handles the low-level details of 
      * writing 
      */ 
      FileWriter theFile = new FileWriter(fileName); 

      /* Create a PrintWriter object to wrap around the FileWriter object */ 
      /* This allows the use of high-level methods like println */ 
      PrintWriter fileOut = new PrintWriter(theFile); 

      /* Print some lines to the file using the println method */ 
      for (int i = 1; i <= userList.size(); i++) { 
       fileOut.println(userList.get(i).getUsername()); 
       fileOut.println(userList.get(i).getPassword()); 
      } 
      /* Close the file so that it is no longer accessible to the program */ 
      fileOut.close(); 
     } 

     /* Handle the exception thrown by the FileWriter methods */ 
     catch (IOException e) { 
      System.out.println("Problem writing to the file"); 
     } 
    } /* End of method writeToFile */ 
  • 的用户列表是使用泛型(ListInterface<User>),如果你不想使用仿制药,你可以只说ListInterface用户列表

    ,徘徊无论它出现一个动态的链接列表。

    • 您的用户类应实现可比性,包括以下所述的方法:
    public int compareTo(User user) { 
    
    } 
    
    public boolean equals(Object user) { 
    
    } 
    
    • 需要注意的是,在你不使用泛型的情况下,可能需要强制转换。否则,您将收到编译错误。
0

当使用String.split()它返回一个String [](阵列)使用字母(正则表达式)你用它发送除以。你需要将它保存在某个地方,否则分割不会做任何事情。所以usrnm:用户名以字符串数组{“usrnm”,用户名}作为参数使用“:”返回。所以你只是这样做:

public void checkCredentials() { 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(new File("accounts.txt"))); 
     String text = br.readLine(); 
     text = text.toLowerCase(); 
     String[] values = text.split(":"); 
     System.out.println(values[1]); // username is the second value in values 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

你只是做同样的下一行缓冲阅读器,密码。

Accounts.txt看起来像这样:

usrnm:USERNAME 
pswrd:PASSWORD 
在seperat线用于与readline的使用方便

();

+0

不应该'checkCredentials'带两个输入参数? (即用户名和密码)并返回布尔值(即,登录是否正确)? – Catchwa

+0

这可能是一个简单的解决方案,只需使用String.equals()进行比较,如果两者都正确,则返回true。 – d0rf3n

+0

是的,我只是说你写的方法(和问题中的方法)实际上并没有任何东西。 – Catchwa

相关问题