简体   繁体   English

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

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

Hi Im using eclipse in Windows 10 OS.嗨,我在 Windows 10 操作系统中使用 Eclipse。 Every time the user logins.I'm checking that particular user id last but one entry in the file and checking if the user is logged out or not.The file entries looks like每次用户登录时,我都会检查该特定用户 ID 最后但文件中的一个条目,并检查用户是否已注销。文件条目看起来像文件结构的图片 Here first parameter is timestamp,second is INFO,third is login or logout status and the numbers 10,1 and 3 are the userid Now i need to check the last but one entry of that particular userid and change the value based on login or logged out.这里第一个参数是时间戳,第二个是 INFO,第三个是登录或注销状态,数字 10,1 和 3 是用户 ID 现在我需要检查该特定用户 ID 的最后一个条目,并根据登录或登录更改值出去。 I'm passing the userid to the function has an arguement so the JAVA code needs to check that particular userid's (which is passed as parameter) last but one entry我将用户 ID 传递给函数有一个争论,因此 JAVA 代码需要检查该特定用户 ID(作为参数传递)的最后一个条目

i have written the java code like this我已经写了这样的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();
        } 

Here I'm passing userid in User() function.What changes should be made so that i will be getting the information of the last but one entry of that particular userid in java.Could anyone please help me with the changes that should be made.在这里,我在 User() 函数中传递用户 ID。应该进行哪些更改,以便我将在 java 中获取该特定用户 ID 的最后一个条目的信息。有人可以帮助我进行应该进行的更改吗.

Try this:尝试这个:

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