我使用此代码 fragment 从网页中读取文本并将其保存为字符串?
我希望 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>
而不是单个 String
。)
关于java - 如何重置 BufferedReader(或 InputStreamReader)以使用 readline() 两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32894808/