I am reading an InputStream (fis) from a source and on which I have to do some multiple search. I am using Scanner class and I instantiate it after every search. But it works on
Create just one Scanner
, and reuse it each time. The problem is happening because the BufferedReader
*buffers* your input -- which means that it reads more than it needs and stores it up for later. When you create your second scanner, all the input has already been grabbed by the first
BufferedReader`, leaving nothing left to scan.
The second print is returning zero.
Because you've already read the stream to EOS counting lines the first time. So when you do it again there are zero lines left to count, so you get zero.
Working as designed.