There is a text file that stores session data:
1542384078773 668873060 236.215.100.166 1542384079774 161963738 194.42.176.2 1542384080774 378627692 37.138.100.42 1542384081774 335983167 254.241.160.5 1542384082774 798168250 68.167.208.123 And so on, here in each line the first number is the time of the session start in a millisecond format (since the beginning of 1970), the second number is a randomly generated 9-bit session ID, the third is an IP address.
The task is to rewrite this file, removing data about sessions older than three days from it, for example.
How can we allocate the first number in each row in order to determine whether we delete the row with this session (it is older than three days) or not?
public void createNewListOfSessions(int ageInDays) { while (scanner.hasNext()) { Date date = new Date(System.currentTimeMillis()); if (Long.parseLong(scanner.nextLine().split(" ").get(0)) > (date.getTime() - 3 * 24 * 60 * 60 * 1000)) { SessionData session = new SessionData(); session.setSessionStartTime(Long.parseLong(scanner.next())); session.setSessionID(scanner.next()); session.setSessionIP(scanner.next()); sessionsCounter++; scannedSessions.add(session); } } } Here I tried to allocate the first number of each line through scanner.nextLine().split(" ").get(0) , but this is incorrect. How to highlight this number?
As a result, the following 100% working method was obtained:
public void createNewListOfSessions(int ageInDays) { while (scanner.hasNext()) { Date date = new Date(System.currentTimeMillis()); String[] current = scanner.nextLine().split("\\s+"); for (String subCurrent : current) { if ("".equals(subCurrent)) { continue; } } Long sessionStartTime = Long.parseLong(current[0].trim()); if (sessionStartTime > (date.getTime() - ageInDays * 24 * 60 * 60 * 1000)) { SessionData session = new SessionData(); session.setSessionStartTime(sessionStartTime); session.setSessionID(current[1]); session.setSessionIP(current[2]); sessionsCounter++; scannedSessions.add(session); } } }