Search code examples
javaline-numbers

How to extract exact lines of text from a text file using line numbers in java?


How to extract exact lines of text from a text file using line numbers in JAVA? For example i have a 1000 lines in a example.txt file from which i want to extract line numbers 100 - 150 to another report.txt file.


Solution

  • Try this,

    while ((input = bufferedReader.readLine()) != null)
    {
      count ++; // use a counter variable
      if (count >= fromLineNumber && count <= endLineNumber)
        {
           // process
           if(counter == endLineNumber) // break the loop while you reach the endLineNumber
            {
               break;
            }
        }
    }