# Thread: How to read current previous and next line from a text file

1. Member
Join Date
Mar 2011
Posts
19
Rep Power
0

## How to read current previous and next line from a text file

I have a script that reads current and next line from a text file and I am trying to modify this script so that it reads three lines (current prev next or current and next two lines ).

Any help is greatley appreciated.

//Construct the BufferedWriter object
File file = new File("C:/text.txt");
bufferedWriter = new BufferedWriter(new FileWriter(file));

line != null; line = next)
{
System.out.println("Current line: " + line);
System.out.println("Next line: " + next);
}

2. Seems like you'll have to read them all in, keeping track of all three and updating them as you go.

3. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
KevinWorkman - 'Keeping track' is where I need help :) I know I may need to use some sort of simple loop to go through the entire file line by line and then to save the three lines into a memory as variables as I found them. But I have no clue on how to go about this.

4. What part of this are you having trouble with? I'm not exactly sure what your goal is, but if you just want to keep track of a previous, current, and next, wouldn't three String variables do the trick?

Although, your "next" variable would actually have to be the last line you read, your "current" variable would be the line read before that, and your "last" would have to be the one read two lines before the last line you read.

5. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
Let us say I am rading one line at a time and my current line (readline) is at line 3 and the file I am reading has 4 lines, so what I am trying to do is that when reading line 3 I need to be able to compare line 3 (current line) with line 2 (prev) and line 4 (next).

So if If I store line3 to three variables when I am reading line 3 I will end up having three variables with same value. I am sure I need to do more than that.

6. When you read a line:

Set previous equal to current.
Set current equal to next.
Set next equal to the line you just read.

7. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
I tried different combinations of the order of the String varaibles, but I still not getting the three expected lines

for (String next, line = reader.readLine(); line != null; line = next) {

System.out.println("Current line: " + line);
System.out.println("Next line: " + next);

String Previous = line;
String Current = next;
String Next = Current;
//next = line;

System.out.println("Prev: " + Previous);
System.out.println("Curr: " + Current);
System.out.println("Next: " + Next);
}
Here are my four lines:
Jacksonville,Florida,200,20110404
Jacksonville,Florida,200,20110405 -->Prev
Jacksonville,Florida,200,20110406 -->Current
Jacksonville,Florida,200,20110405 -->Next

8. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
Here is a working script. This script reads four line at a time:

Java Code:
        String line;
String line1;
String line2;
String line3;
try {

//String line1; // String that holds current file line
int count = 0;	// Line number of count
int count1 = 0;
int count2 = 0;
int count3 = 0;
// Read first line
count++;
count1++;
count2++;
// Read through file one line at time. Print line # and line
while (line != null){
System.out.println("1st Line:" + count+": "+line);

System.out.println("2nd Line: " +count1+": "+line1);
System.out.println("3rd Line: " +count2+": "+line2);
System.out.println("4rd Line: " +count3+": "+line3);
count++;
}

}catch (ArrayIndexOutOfBoundsException e){
System.out.println("Usage: java ReadFile filename\n");

}catch (IOException e){
// If another exception is generated, print a stack trace
e.printStackTrace();
}
Last edited by Fubarable; 04-21-2011 at 10:26 PM. Reason: code tags added

9. Originally Posted by emyk
Here is a working script. This script reads four line at a time:

Wouldn't it be more practical to read one line at a time?

Also, I added code tags added to your post above to help make it readable.

To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Java Code:
[cod&#101;]
// your code goes here
// notice how the top and bottom tags are different
[/cod&#101;]
Best of luck

10. And also, what if the file's lind count isn't a multiple of three?

11. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
Fubarable - that explains why it is only reading 12 lines while the file I am reading has 16 lines :) Now I back to square one.

12. Myself, I'd do what the others recommend, read one line at a time and set the three String variables accordingly. But you can only have the current line and the previous two lines, not the next line [edit: as Kevin already mentioned]. The only way to get the "next" line that I can think of is to read all of the lines into an ArrayList<String> and then after your done, iterate through the list getting the ith line, the (i-1)th line, and the (i+1)th line, assuming that you've accounted for both edges -- the beginning and the end of the list.
Last edited by Fubarable; 04-22-2011 at 12:20 AM.

13. Member
Join Date
Mar 2011
Posts
19
Rep Power
0
Thanks - I wil try your suggestion.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•