'Read last line of a text file using Java [duplicate]

I am fairly new to Java and I would like to ask how is it possible to get the last line of a text file using Java? For example, the contents of the text file would be something like:

001,abc,[email protected],123456,President

002,xxx,[email protected],xxx,Member

What I am trying to get is the userID of the users (eg:001). I already know how to read from a text file normally, but what I need is the last line from the text file so that I can give users incrementing IDs.



Solution 1:[1]

try (ReversedLinesFileReader reader = new ReversedLinesFileReader(new File("path/to/file.txt"), StandardCharsets.UTF_8)) {
    System.out.println("Last line is: " + reader.readLine());
} catch (IOException e) {
    // ignore for now
}

It should work perfect

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexey Surovenko