Java Scanner with InputStream not working

后端 未结 2 2024
终归单人心
终归单人心 2021-01-28 22:51

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

相关标签:
2条回答
  • 2021-01-28 23:19

    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 firstBufferedReader`, leaving nothing left to scan.

    0 讨论(0)
  • 2021-01-28 23:30

    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.

    0 讨论(0)
提交回复
热议问题