gpt4 book ai didi

java - 如何从JAVA文件中读取特定用户ID的最后一个条目

转载 作者:行者123 更新时间:2023-12-01 17:47:52 25 4
gpt4 key购买 nike

嗨,我在 Windows 10 操作系统中使用 eclipse。每次用户登录时。我都会检查该特定用户 ID 的最后一个条目,并检查该用户是否已注销。文件条目看起来像 The picture of how the file structure looks like这里第一个参数是时间戳,第二个是INFO,第三个是登录或注销状态,数字10,1和3是用户ID现在我需要检查该特定用户 ID 的最后一个条目,并根据登录或注销更改该值。我将用户 ID 传递给函数有一个争论,因此 JAVA 代码需要检查特定用户 ID(作为参数传递)的最后一个条目

我写了这样的java代码

public static boolean User(String userid) {

try {
String acc = userid;
File file = new File("C:\\Temp\\log\\bank.log");
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] substrings = data.split("[:]");
if (substrings[5].contains(acc) && substrings[4].contains("Login Successful for user")) {
a = true;
} else {
a = false;
}

}
} catch (Exception e) {
e.printStackTrace();
}

这里我在 User() 函数中传递用户 ID。应该进行哪些更改,以便我能够获取 java 中该特定用户 ID 的最后一个条目的信息。有人可以帮助我进行更改吗?应该做。

最佳答案

试试这个:

public static boolean isLogin(String userId) {
try {
File file = new File("C:\\Temp\\log\\bank.log");
Scanner myReader = new Scanner(file);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] substrings = data.split("[:]");
if (substrings[5].contentEquals(" " + userId) && substrings[4].contentEquals(" Login Successful for user")) {
return true;
} else {
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

关于java - 如何从JAVA文件中读取特定用户ID的最后一个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60834917/

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