How to read a specific line using the specific line number from a file in Java?

For small files:

String line32 = Files.readAllLines(Paths.get("file.txt")).get(32)

For large files:

try (Stream<String> lines = Files.lines(Paths.get("file.txt"))) {
    line32 = lines.skip(31).findFirst().get();
}

Leave a Comment