如何重置BufferedReader(或InputStreamReader)以使用readline()两次?

时间:2015-10-01 18:52:58

标签: java android stream

我使用此代码段从网页中读取文本并将其保存为字符串?

我希望readline()函数从beggining开始。所以它会再次阅读网页的内容。我怎么能这样做

if (response == httpURLConnection.HTTP_OK) {

                in = httpURLConnection.getInputStream();
                isr = new InputStreamReader(in);
                br = new BufferedReader(isr);

                while ((line = br.readLine()) != null) {
                    fullText += line;

                }

                // I want to go through a webpage source again, but
                // I can't because br.readLine() = null. How can I                   put 
                // put a marker on the beginning of the page? 
                while ((line1 = br.readLine()) != null) {
                    fullText1 += line1;
                // It will not go into this loop
                }

2 个答案:

答案 0 :(得分:2)

如果Reader返回reset(),您只能标记markSupported的位置(并使用true返回),我非常怀疑该流返回由httpURLConnection.getInputStream()支持标记。

我认为,最佳选择是将响应读入缓冲区,然后您可以在该缓冲区上创建任意数量的读取器。您需要包含行终止字符(您当前正在丢弃)以保留行结构。 (或者,您可以将回复读入List<String>而不是单个String。)

答案 1 :(得分:0)

来自InputStream will not reset to beginning

  

您在BufferedInputStream对象中的流,如:   如果您的InputStream实际上支持使用mark,则使用markSupported()方法。根据API,InputStream类没有,但java.io.BufferedInputStream类却没有。也许你应该将你的流嵌入到BufferedInputStream对象中,如:

InputStream data = new BufferedInputStream(realResponse.getEntity().getContent());
// data.markSupported() should return "true" now
data.mark(some_size);
// work with "data" now
...
data.reset();