나는 몇 가지 코드를BufferedReader를 재설정하지 않는 방법은 무엇입니까?
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
다시 하지만 seconed 루프, 그것은 시작 첫 번째 루프 끝 위치에서
때로는 그런 seconed 루프에서 아무것도 읽을 수 없습니다 :
그래서 문제가 무엇을
가끔 그런 결과를 출력한다?
왜 당신이하고있는 마지막 while 루프 바로 전에 this : reader = new BufferedReader (...); –