Hi,
i'm a beginner in Java.
i'm just trying to code a program that extract some specificline from a text file.
for example :
i want to extract line No 5000 and lineNo 5100 and lineNo 5200.

Here is my code, but it only extract just one line from my text file.
Actually i can extract only one line. (LineNo = 5134)
thank you very much.

import java.io.*;

public class ReadSpecificLine
{

public static void main(String[] args){
String line = "";
int lineNo;
try
 {
FileReader fr = new FileReader("C:\\IMS.txt");
BufferedReader br = new BufferedReader(fr);
for(lineNo=1;lineNo<10700;lineNo++)
{
if(lineNo==5134)
{

line = br.readLine();
}

else br.readLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Line: " + line);
}
}

First of all I find it surprising that you wrote this code and still couldn't find the answer.
I mean was it so difficult to add this to your code?

if (lineNo==5134)
{
  line = br.readLine();
} else if (lineNo==5100)
{
  line = br.readLine();
} else if (lineNo==5200)
{
  line = br.readLine();
}
..
..
..
 else {
  br.readLine();
}

Of course you code prints only one line because you have only ONE variable "line". If you want to print all of them either add a
"System.out.println" at the place that you read them:

if (lineNo==5134)
{
  line = br.readLine();
  System.out.println(line);
} else if (lineNo==5100)
{
  line = br.readLine();
  System.out.println(line);
}

Or declare an array that will store the line that you want:

String [] lines = new String[N];
int i=0;
.....
if (lineNo==5134)
{
  line = br.readLine();

  lines[i] = line; 
  i++;
} else if (lineNo==5100)
{
  line = br.readLine(); 

  lines[i] = line; 
  i++;
}

But that wouldn't be quite correct.I have noticed the way you read the file:
>> for(lineNo=1;lineNo<10700;lineNo++)
It is wrong. In general you wouldn't know how many lines the file has. So instead of the for loop use this:

FileReader fr = new FileReader("C:\\IMS.txt");
BufferedReader br = new BufferedReader(fr);
int lineNo = 0;

String line = br.readLine(); 
lineNo++;

while (line!=null)
{
  // do whatever you want with the line or the line number
System.out.println("Line Number: "+lineNo+", Line: "+line);

  line = br.readLine();
  lineNo++;
}

Whenever you read a new line you increase the lineNo. If the file has no more lines then "line" will be null and you will exit the loop. Remember that the command: line = br.readLine(); must always be the last command of the loop. In this case it acceptable to add this: lineNo++; since it doesn't affect anything, just increases the counter.
Also notice that when you exit the loop the "line" would be null but the "lineNo" would have been increased on last time at the last run. So the total number of lines the file has after you exit the loop would be "lineNo - 1", because the last "br.readLine()" returned null but the "lineNo++" was increased anyway

commented: OP didn't mark as solved, although it was solved. +8

Thank you very much sir, i made a small mistake in my code with IF..ELSE block.
now it's working well.
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.