Java 如何从输入文件中读取指定字符串,然后读取其后面的下一个单词
在Java中,我希望能够有两个接受字符串的JTextField。一个是用户名,另一个是密码。若要“登录”,程序将搜索-->accounts.txt文件,并首先搜索“usrnm:”,一旦找到它,它将在以下内容后使用单词: 密码也是如此,但它会搜索“pswrd:” 以下是到目前为止iv'e得到的信息:Java 如何从输入文件中读取指定字符串,然后读取其后面的下一个单词,java,passwords,bufferedreader,login-system,Java,Passwords,Bufferedreader,Login System,在Java中,我希望能够有两个接受字符串的JTextField。一个是用户名,另一个是密码。若要“登录”,程序将搜索-->accounts.txt文件,并首先搜索“usrnm:”,一旦找到它,它将在以下内容后使用单词: 密码也是如此,但它会搜索“pswrd:” 以下是到目前为止iv'e得到的信息: public void checkCredentials() { try { BufferedReader br = new BufferedReader(new Fi
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();
}
}
感谢您的帮助 与其搜索文件两次,不如先保存用户名,然后在下一行保存密码。当您阅读它们时,您可以创建具有以下属性(用户名和密码(字符串))的用户对象。此外,您还可以在系统处于活动状态时将其保存在链接列表中。以便您可以轻松访问用户帐户。一旦用户需要登录,您将扫描列表,并使用userList.get(i).equals(textfield.getText())确定所请求的用户。之后,如果上述语句为true,您将检查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 */
- 注意,如果您不使用泛型,可能需要类型转换。否则,将出现编译错误
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
在单独的行中,便于与readline()一起使用 如果我的密码是“usrnm:”?不应该
checkCredentials
接受两个输入参数吗?(即用户名和密码)并返回布尔值(即登录是否正确)?这可能是一个简单的解决方案,只需使用String.equals()比较它们,如果两者都正确,则返回true。是的,我只是说您编写的方法(以及问题中的方法),实际上什么都没做。这并不是对你的帖子或任何事情的反对我刚把原来的一个东西做了个例子。它使用拆分键和类似属性的键打印出值。因此,这是一个例子来解释,一个开始。我只是在你的评论中补充了一些东西,以防专栏文章读到你想知道你的意思。:)我想这可能不是不言自明的。我认为,仅仅陈述一些事情而不解释,并不是很有帮助因此,我把你的问题当作一个问题,而不是一个陈述。虽然你的第二篇文章,我想就像我刚才解释的那样。
usrnm:USERNAME
pswrd:PASSWORD