简体   繁体   中英

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

Hi Im using eclipse in Windows 10 OS. 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文件结构的图片 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. 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

i have written the java code like this

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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