I am reading a large file as such:
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.skip(1).forEach(Main::parseLine);
} catch (IOException e) {
e.printStackTrace();
}
where parse line looks like this:
private static void parseLine(String line) {
System.out.println(line);
}
what I would like to do is also keep track of the current line I am reading and have parseLine
look like this instead:
private static void parseLine(String line, int idx) {
System.out.println(line);
}
How can I do this?
Collect the lines to a list then use IntStream.range
or an explicit for
loop to call your method passing the element along with the index:
List<String> result = stream.skip(1).collect(Collectors.toList());
IntStream.range(0, result.size())
.forEach(i -> parseLine(result.get(i), i));
Another option would be to use the LineNumberReader API.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments