reading between two lines from a text file in java

Hi,I have text file and i want to read information present between two lines.Can i know how this can be done in java.ThanksMurali
[164 byte] By [muralikrishna_mka] at [2007-10-3 2:51:58]
# 1
>Can i know how this can be done in java.If you can tell me how to do that in real life (or in some other programming language)
IanSchneidera at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 2
Oh, you mean two lines which are not consecutive?
IanSchneidera at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 3
yes, i mean between two lines which are not consecutive
muralikrishna_mka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 4
Find the first line, start buffering lines in an array or List, and keep reading until you find the last line.
IanSchneidera at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 5
> yes, i mean between two lines which are not> consecutiveI might be making this to simple but:1. Ignore everything till you get to the first line2. Read data till you get the second line.3. EndIs there a specific part you are stuck on?
zadoka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 6

BufferedReader in = new BufferedReader(new FileReader("Results.log"));

while((str=in.readLine())!=null){

if(str.startsWith("APDU")){

list.add(str);

System.out.println(str);

}

}

In the above code snippet i have provided the condition for the first line.

How to specify the condition for the second line

muralikrishna_mka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 7

> BufferedReader in = new BufferedReader(new

> FileReader("Results.log"));

>

> while((str=in.readLine())!=null){

> if(str.startsWith("APDU")){

> list.add(str);

> System.out.println(str);

> }

>

> }

> above code snippet i have provided the condition for

> the first line.

> ow to specify the condition for the second line

Please use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting

Tags[/url].

This code just reads the first line. You want to keep reading until you get to different line, right?

zadoka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 8
The above code just reads the first line. I want to keep reading until i get to different line. Can any one suggest me a way to do this
muralikrishna_mka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 9

Inside your while loop you will want to something like this:

if(isThisSecondLine()){

break;

}

if(hasFirstLineOccurred()){

doSomethingWithThisLine();

}

zadoka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks for your reply.
muralikrishna_mka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 11

BufferedReader in = new BufferedReader(new FileReader("Results.log"));

while((str=in.readLine())!=null){

if(str.startsWith("APDU")){

if(str.startsWith("Time"))

break;

else

System.out.println(str);

}

}

I have given the above statements to print the information between two non-consecutive lines. But it's not working.

Can any one suggest me a way to do this.

muralikrishna_mka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 12
This:if(str.startsWith("Time")) will never be true if it is inside the if statement:if(str.startsWith("APDU")){
zadoka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 13

BufferedReader in = new BufferedReader(new FileReader("Results.log"));

boolean logging = false;

while((str=in.readLine())!=null){

if(str.startsWith("APDU")){

logging = true;

list.add(str);

System.out.println(str);

}

else if(logging) {

list.add(str);

System.out.println(str);

if(str.startsWith("END"))

logging=false;

}

}

You'll, of course, need to replace END with whatever you're looking for to end the log. And if you want to end the while loop immediately after it reads the END part, just change that statement to:

if(str.startsWith("END")) {

logging = false;

break;

}

technodolta at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 14

BufferedReader in = new BufferedReader(new FileReader("Results.log"));

boolean logging = false;

while((str=in.readLine())!=null){

if(str.startsWith("APDU")){

logging = true;

list.add(str);

System.out.println(str);

}

else if(logging) {

list.add(str);

System.out.println(str);

if(str.startsWith("END"))

logging=false;

}

}

The above code worked fine with one small change.

after the statement

if(str.startsWith("END"))

we should add break;

instead of logging=false;

Thanks

muralikrishna_mka at 2007-7-14 20:40:52 > top of Java-index,Java Essentials,Java Programming...
# 15
HelloHow can i read between two particular string from a text file using java.Best RegardsHuma
HumaKhana at 2007-7-21 10:01:05 > top of Java-index,Java Essentials,Java Programming...