我使用这个代码片段从网页中读取文本,并将其保存为字符串?
我希望readline()函数从头开始。所以它会再次读取网页内容。我该怎么做呢
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
}
如果markSupported
返回true
,则只能标记Reader
的位置(并返回reset()
),并且我非常怀疑httpURLConnection.getInputStream()
返回的流是否支持标记。
我认为,最好的选择是将响应读入缓冲区,然后您可以在该缓冲区上创建任意数量的读取器。您将需要包括行终止字符(您当前正在丢弃)以保留行结构。(或者,您可以将响应读入List<String>
而不是单个String
。)
From InputStream将不会重置为开始
你的流在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();