gpt4 book ai didi

java - 如何不重置BufferedReader?

转载 作者:行者123 更新时间:2023-12-01 12:19:36 25 4
gpt4 key购买 nike

我写了一些代码

public static void main(String[] args) throws HttpException, IOException,
JSONException {
// TODO Auto-generated method stub
try {
URL murl = new URL(
"http://www.baidu.com/link?url=NaethV_J2hSPVx_OdPlHk73964mU4LcwWkJmVUV4vIkuCXRf1y09ufRZVwkHJqSAa2mMSCoTLYVhGv2AyV_04_");
HttpURLConnection conn = (HttpURLConnection) murl.openConnection();
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");
conn.setConnectTimeout(10000);

conn.connect();

String strencoding = null;


Map<String, List<String>> map = conn.getHeaderFields();
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();


String key = null;
String tmp = null;
while (iterator.hasNext()) {
key = iterator.next();
tmp = map.get(key).toString().toLowerCase();


if (key != null && key.equals("Content-Type")) {
System.out.println(tmp);
int m = tmp.indexOf("charset=");
if (m != -1) {
strencoding = tmp.substring(m + 8).replace("]", "");

}
}
}
strencoding = strencoding == null ? "UTF-8" : strencoding;

conn.getResponseCode();
// conn.connect();
String href = conn.getURL().toString();
System.out.println(href);
href = href.replace("http://", "");
try {
href = href.split("/")[0];
} catch (Exception eee) {

}
/*
* ParseDomainName pdn = new ParseDomainName(href);
* System.out.println("Your host IP is: " +
* pdn.getMyIP().getHostAddress());
* System.out.println("The Server IP is :" +
* pdn.getServerIP().getHostAddress()); // InputStream inputstream =
* conn.getInputStream();
*/

BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), strencoding));

String lines;
int i = 1;
while ((lines = reader.readLine()) != null) {

if (lines.toLowerCase().indexOf("charset") > 0) {
System.out.println(lines);
String strtmp = lines;
int inttmp = strtmp.indexOf("charset");
if (inttmp > -1) {
System.out.println(strtmp.length());
strencoding = strtmp
.substring(inttmp + 7, strtmp.length())
.replace("=", "").replace("/", "")
.replace("\"", "").replace("\'", "")
.replace(" ", "").replace("<", "")
.replace(">", "");
break;
}
}
i++;
}

reader.mark(0);
reader.reset();
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), strencoding));

while ((lines = reader.readLine()) != null) {
System.out.println(i + " " + lines);
if (lines.toLowerCase().indexOf("icp") > 0) {
// System.out.println(i + " " + lines);
}
i++;
}
System.out.println(i + "---" + strencoding);
reader.close();
conn.disconnect();
} catch (Exception e2) {
e2.printStackTrace();

}

}

最后一个while循环,在第一个循环中,我检查页面字符集并中断,然后我重置阅读器,然后再次readLine,但在第二个循环中,它从第一个循环结束的位置开始。

有时它会打印这样的结果: enter image description here

有时在第二个循环中不读取任何内容,如下所示: enter image description here

那么问题出在哪里呢?

最佳答案

您必须在第一个 while 循环之前调用 reader.mark() ; reader.mark() 本质上保存了阅读器的当前位置,以便您在调用 reader.reset() 时可以返回到该位置。

您也不想将 0 传递给 reader.mark()。请参阅以下参数的 java 规范:

readAheadLimit - 在保留标记的同时限制可以读取的字符数。读取字符达到或超过此限制后尝试重置流可能会失败。大于输入缓冲区大小的限制值将导致分配一个大小不小于限制的新缓冲区。因此应谨慎使用较大的值。

(也就是说,传入0是没有用的,需要传入一个大于mark()和reset()之间读取的字符数的数字)。

关于java - 如何不重置BufferedReader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com