繁体   English   中英

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

[英]How to read the last but one entry of the particular userid from file in JAVA

嗨,我在 Windows 10 操作系统中使用 Eclipse。 每次用户登录时,我都会检查该特定用户 ID 最后但文件中的一个条目,并检查用户是否已注销。文件条目看起来像文件结构的图片 这里第一个参数是时间戳,第二个是 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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM