I have a text file with a rather large amount of data of about 2,000,000 lines. Going through the file with the following code snippet is easy but that's not what I need ;-)
def f = new File("input.txt")
f.eachLine() {
// Some code here
}
I need to read only a specific range of lines from the file. Is there a way to specify the start and end line like this (pseudo-code)? I'd like to avoid loading all lines into memory with readLines() before selecting the range.
// Read all lines from 4 to 48
def f = new File("input.txt")
def start = 4
def end = 48
f.eachLine(start, end) {
// Some code here
}
If this is not possible with Groovy any Java solution is welcome as well :-)
Cheers, Robert
The Java solution:
BufferedReader r = new BufferedReader(new FileReader(f));
String line;
for ( int ln = 0; (line = r.readLine()) != null && ln <= end; ln++ ) {
if ( ln >= start ) {
//Some code here
}
}
Gross, eh?
Unfortunately unless your lines are fixed length, you're not going to be able to skip to the start
th line efficiently since each line could be arbitrarily long and therefore all data needs to be read. That doesn't preclude a nicer solution though.
Thought it was worth an update to show how to do this efficiently with Streams:
int start = 5;
int end = 12;
Path file = Paths.get("/tmp/bigfile.txt");
try (Stream<String> lines = Files.lines(file)) {
lines.skip(start).limit(end-start).forEach(System.out::println);
}
Because Streams are lazily evaluated, it will only read lines up to and including end
(plus whatever internal buffering it chooses to do).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With