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