gpt4 book ai didi

java - 如何从输入文件中读取指定的字符串,然后读取它之后的下一个单词

转载 作者:行者123 更新时间:2023-11-30 08:27:15 24 4
gpt4 key购买 nike

在 Java 中,我希望能够有两个接受字符串的 JTextField。一个是用户名,另一个是密码。要“登录”,程序将搜索文件 --> accounts.txt 并首先搜索“usrnm:”,一旦找到它,它会在 :密码也是如此,但它会搜索“pswrd:”。

这是我到目前为止得到的:

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

感谢任何帮助!

最佳答案

与其搜索文件两次,不如先保存用户名,然后在下一行保存密码。在阅读它们时,您可以创建具有以下属性(用户名和密码 (String))的用户对象。您也可以在系统处于 Activity 状态时将它们保存在链表中。这样您就可以轻松访问用户帐户。一旦用户需要登录,您将扫描列表并使用 userList.get(i).equals(textfield.getText()) 确定用户询问。之后,如果上述声明成立,您将检查 userList.get(i).getPassword().equals(textfield2.getText()) 是否为真,并相应地授予或拒绝访问权限。

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

    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 */
  • userList 是一个使用泛型的动态链表(ListInterface<User>)

    如果你不想使用泛型,你可以只说 ListInterface userList,无论它出现在哪里。

    • 您的 User 类应实现可比较的并包括以下方法:
    public int compareTo(User user) {

    }

    public boolean equals(Object user) {

    }
    • 请注意,如果您不使用泛型,则可能需要进行类型转换。否则,你会得到编译错误。

关于java - 如何从输入文件中读取指定的字符串,然后读取它之后的下一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20961522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com