Manav Veer Gulati Manav Veer Gulati - 1 year ago 111
Java Question

Reading File using Java given a starting and an ending line number

I am new with Java Programming and i wasn't able to find an answer to my problem anywhere.

How to read few lines of a file and store it in a String(or a list of Strings).

For example from a file of 1000 lines i need to read only 200 lines starting from 200th line to 400th line.

I came across FileInputStream using it we can define the starting position but we cannot define the ending position.

Answer Source

You can't do this directly. You can only do this by reading and ignoring the first lines you don't care about.

You can do that with Java's LineNumberReader. Use it to read the file, reading one line at a time. Keep reading and ignoring lines until you get to line 200, start processing the data, stop once you reach 400.

Note: Before you ask, no, LineNumberReader#setLineNumber does not change the file position, it just artificially sets the reported line number:

By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and can be changed with a call to setLineNumber(int). Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

Another option is to just use a BufferedReader, calling readLine() 199 times to skip to the 200th lines, then reading the next 200 (or whatever) lines. But, LineNumberReader just conveniently keeps track of the line number for you.

A third option, since Java 8, is to use the streams API and do something like:

  .skip(200)   // skip to line 200
  .limit(200)  // discard all but the 200 lines following this

Passing your processing Consumer to forEach().

Either way, same concept: You have to read and discard the first N lines of the file, you can't get around that.

Example with LineNumberReader:

LineNumberReader reader = new LineNumberReader(new FileReader("input.txt"));

for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    if (reader.getLineNumber() > 400) {
        break; // we're done
    } else if (reader.getLineNumber() >= 200) {
        // do something with 'line'

Example with BufferedReader, not quite as straightforward as the above:

BufferedReader reader = new BufferedReader(new FileReader("input.txt"));

// skip to the 200th line
for (int n = 0; n < 199 && reader.readLine() != null; ++ n)

// process the next 201 (there's 201 lines between 200 and 400, inclusive)
String line;
for (int n = 0; n < 201 && (line = reader.readLine()) != null; ++ n) {
    // do something with 'line'

And an example using Files is already given above.

How you want to organize the conditions and test for EOF and such in your for or while or whatever loops is more a matter of personal taste, those are just arbitrary illustrative examples.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download