How to extract a specific line from a text file?
View Answers
July 30, 2009 at 12:39 PM
Hi Friend,
Try the following code:
import java.io.*;
public class ReadSpecificLine{
public static void main(String[] args){
String line = "";
int lineNo;
try {
FileReader fr = new FileReader("C:\\Employee.txt");
BufferedReader br = new BufferedReader(fr);
for(lineNo=1;lineNo<10;lineNo++){
if(lineNo==5){
line = br.readLine();
}
else br.readLine();
}
}
catch (IOException e){
e.printStackTrace();
}
System.out.println("Line: " + line);
}
}
Hope that it will be helpful for you.
Thanks
July 31, 2009 at 2:11 AM
Thank you very much friend.
i will continue my experience with this example.
i will comeback to you later.
Best Regards
August 4, 2009 at 2:47 AM
Now i try to extract one more line from my text file, here is my code but it's not working, why please?
#
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
if(lineNo==5100)
line = br.readLine();
{
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Line: " + line);
}
}
#
August 19, 2009 at 3:13 PM
Hi friend,
thank you very much for the solution.
SPX