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.
Asked
Active
Viewed 2,593 times
-3

Arun Bertil
- 4,598
- 4
- 33
- 59

sisir
- 111
- 2
- 13
-
What is your attempt ? – Suresh Atta Sep 25 '13 at 10:00
3 Answers
0
You can have a simple solution by iterating using bufferedreader.readline() ignoring the first unneeded, and then write the needed ones to the new file.

mjd
- 1
0
Try following.
File file = new File("pathto your file");
int readStart = 100;
int readEnd = 150;
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int currentLineNumber = 0;
while ((((line = br.readLine()) != null)&& (currentLineNumber <= readStart))){
currentLineNumber = currentLineNumber + 1;
}
while (((line = br.readLine()) != null)
&& (currentLineNumber >= readStart && currentLineNumber <= readEnd)) {
// process the line.
currentLineNumber = currentLineNumber + 1;
}
br.close();
Hope it helps

Jabir
- 2,776
- 1
- 22
- 31
0
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;
}
}
}

newuser
- 8,338
- 2
- 25
- 33