-1

I am new to java so now I am using buffer reader in my program once I end the Loop the loop will break but again In the second loop I have called the same Buffer Reader now again I want to read from the first line so help me. Now It Is reading as Null since It has Reached End of the Line

while ((s2 = br.readLine())!=null) {
    if (s2.contains(keyword2)) {
        s2 = s2.replaceAll(keyword2, " ");
        Alternate_Account_Number = s2;                    
        System.out.println(Alternate_Account_Number);
        break;
     }

}

String s3;  

String Meter_Number = null;

String keyword3 = arra.get(8);

while ((s3 = br.readLine()) != null) {
    if (s3.contains(keyword3)) {
         s3 = s3.replaceAll("\\D+", " ");
         Meter_Number = s3;
         System.out.println(Meter_Number);
         break;
     }

}
Deo
  • 45
  • 8
  • Possible duplicate of [Reset buffer with BufferedReader in Java?](https://stackoverflow.com/questions/5421653/reset-buffer-with-bufferedreader-in-java) – vincrichaud Feb 14 '19 at 09:31
  • If the source is small, you could create a mark at the very beginning and call `br.reset` after having read all lines for the first time (not within the while-loop as you stated). Otherwise, you might as well simply reopen the reader after the first loop – Robert Kock Feb 14 '19 at 09:33
  • When you get to the second loop, the input is already exhausted, so it will never excpectingsxcdptions, unless you hard previously executed `mark()`, which isn't stated here, and which requires that the mark position never gets more than the buffer size behind the current position, as stated in the Javadoc, – user207421 Feb 14 '19 at 10:05

2 Answers2

1

// set the mark at the beginning of the buffer

bufferedReader.mark(0);

// read through the buffer here...

// reset to the last mark; in this case, it's the beginning of the buffer

bufferedReader.reset();

Reset buffer with BufferedReader in Java?

Daniel
  • 951
  • 7
  • 20
  • Do with my code I am not getting I have tried like u only still buffer reader is not recognizing @Daniel – Deo Feb 14 '19 at 12:28
  • see in the first loop I am trying to read the text file using buffer reader and now In the second loop Again I want to read the buffer reader from starting point In the second loop now I cleared ur doubt I think now please help @Daniel – Deo Feb 15 '19 at 02:37
0

You can use mark(int readAheadLimit) Marks the present position in the stream.

You can use close() Closes the stream and releases any system resources associated with it. And read it again.(do as per the requirement)

reset() Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().

Read More

jay sedani
  • 57
  • 9
  • still haven't got can you explain with some example – Deo Feb 14 '19 at 09:40
  • Sorry, I can't provide you the exact code. But you can refer this link for how to use. : **https://www.tutorialspoint.com/java/io/bufferedreader_mark.htm** and use it as per your requirements. – jay sedani Feb 14 '19 at 09:44