I've developed function which allows to skip to a desired line number within a text file (see function).

However, what I would like to do is develop a function which takes me to the beginning of the file (i.e. line 0) after calling the navigateForwardToLine function.
However, I can't seem to work out how to do this.

Would anyone have any suggestions on how do this.

Thanks in advance for your help.

/************************************************************************/
   ...
   ...

   reader = new InputStreamReader(new FileInputStream(this.fileName));
   lnr    = new LineNumberReader(reader);

   private void navigateForwardToLine(int lineNum)
   {
      int currentLine = lnr.getLineNumber();

      try
      {
         for( int intCurrentLine = currentLine; intCurrentLine < lineNum; intCurrentLine++)
         {
              lnr.readLine();
         }
      }
      catch(IOException e)
      {
            e.System.out.println(e.getMessage());
      }
   }
   /************************************************************************/

Recommended Answers

All 14 Replies

Change the relational expression of for loop,

for( int intCurrentLine = currentLine; intCurrentLine < lineNum + currentLine; intCurrentLine++) {
              lnr.readLine();
 }

Thanks for your reply I will try that.

Thanks!!

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.
I think i have to use your method but how is it possible?

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);
}
}

I would suggest spalax to start a new thread with your question. This thread is for answering robben's questions not yours. Should we ignore robben who created this thread in order to answer your question?

No we should not ignore the Roben's thread.
i started a new one.
Thanks

adatapost I've tried you suggestion however it does not seem to work.

After calling navigateForwardToLine, when I want to go back to the beginning of the file (line number 0) I'm currently creating a new InputStreamReader and LineNumberReader (see code below) which seems to work although I'm wondering whether or not it is the most efficient way to do this.

reader = new InputStreamReader(new FileInputStream(this.fileName));
lnr = new LineNumberReader(reader);

I therefore would like to get the naviagteBackToLine function working to remove newing the LineNumberReader object all the time through my code. However, lets say I'm on the last line of the file and I call lnr.readLine() it does not loop back to the beginning of the file. I was therefore wondering how do I go back in a file.

Thanks for your help.

I've developed function which allows to skip to a desired line number within a text file (see function).

However, what I would like to do is develop a function which takes me to the beginning of the file (i.e. line 0) after calling the navigateForwardToLine function.
However, I can't seem to work out how to do this.

Would anyone have any suggestions on how do this.

Thanks in advance for your help.

/************************************************************************/
   ...
   ...

   reader = new InputStreamReader(new FileInputStream(this.fileName));
   lnr    = new LineNumberReader(reader);

   private void navigateForwardToLine(int lineNum)
   {
      int currentLine = lnr.getLineNumber();

      try
      {
         for( int intCurrentLine = currentLine; intCurrentLine < lineNum; intCurrentLine++)
         {
              lnr.readLine();
         }
      }
      catch(IOException e)
      {
            e.System.out.println(e.getMessage());
      }
   }
   /************************************************************************/

Hi

I just want to make it clear, what is it exactly you put in here

private void navigateForwardToLine[B](int lineNum[/B])

and here.

[B]int currentLine[/B] = lnr.getLineNumber();

and what output do you get when you try to navigate to line 0 ?

Sorry don't understand your question!

The int variable lineNum does it hold the number of the line you want to navigate to or the amount of lines in the text, or something else?

private void navigateForwardToLine[B](int lineNum[/B])

The int variable currentLine, what exactly is the information it gets from lnr.getLineNumber? Is it the line you are standing on right now or the line you are trying to reach, or something else?

[B]int currentLine[/B] = lnr.getLineNumber();

When you try to reach the first line (line 0) , what happens?

The int variable lineNum holds the number of the line I want to navigate to.

variable currentLine - The line I'm currently on.

When you try to reach the first line (line 0) , what happens?
Nothing, it just moves to the end line of the file.

Opening with a new buffered reader, opens the file again. You may want to read your file data into an ArrayList and then work with it--depending on how large your file is I suppose.

If you really want to work directly with the file and be able to change file pointer position use "java.io.RandomAccessFile":
http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html


To time your function use: "System.currentTimeMillis()". Store the "start time" and "end time". Subtract the two and you'll have your execution time.

Also see the following article about performance tuning: http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/

The int variable lineNum holds the number of the line I want to navigate to.

variable currentLine - The line I'm currently on.

When you try to reach the first line (line 0) , what happens?
Nothing, it just moves to the end line of the file.

Oki =)

I am not sure this will do it for you. I do not have enough of your code to test it in its context but what I am thinking when testing your loop is that you have made this loop:

for( int intCurrentLine = currentLine; intCurrentLine < lineNum; intCurrentLine++)

When you then want to navigate to line number 0 and sets lineNum to 0 then intCurentLine can not be < lineNum. This means the loop condition is not filled and then you never enter it. If you instead makes intCurentLine <= lineNum you will enter the loop and reach the first line.

I hope this will give you at least some help.
Good luck =)

Hi Sneaker,

This is the code I have at the moment:

private void navigateBackToLine(int intLineToGoTo)
   {
      int intInitialLine = lnr.getLineNumber();

      try
      {
         for( int intCurrentLine = intInitialLine; intCurrentLine < intLineToGoTo + intInitialLine; intCurrentLine++)
         {
              lnr.readLine();
         }
      }
      catch(IOException ioe)
      {
            ioe.printStackTrace();
      }
   }

However, it never moves to line 0 in the file.
I'm not sure I quite get what you saying in your last thread, what should I change in this function?

Thanks.

Oki, let me explain it like this instead...
This is your code:

for( int intCurrentLine = currentLine; intCurrentLine < lineNum; intCurrentLine++)

When you want to navigate to line number 0 the value you give to lineNum is 0.

In your code above you declare that intCurrentLine < lineNum
In words this means that as long as intCurrentLine is smaller than lineNum the if statement shall be executed.

But if lineNum has value 0 then intCurrentLine can not be smaller than lineNum beacuse the smallest value intCurrentLine can have is 0.

So in the case when lineNum has value 0 the if statement in your code will not be executed.

If you want the if statement to execute you have to say as long as intCurrentLine is smaller than, or has the same value as lineNum the if statement shall be executed.

This can be written by combining < and = like this:

for( int intCurrentLine = currentLine; intCurrentLine <= lineNum; intCurrentLine++)

Hope this will help =)

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.