用下面一段代码去抓取网页,读取到人名的时候,如果是两个字的人名就正常,三个字就出乱码,何故?
还有,保存到本地的文件会少一段内容,又是什么原因??代码可以运行
import java.io.*;
public class web {
public static String getWebCon(String domain) {
// System.out.println("开始读取内容...("+domain+")");
File f=new File("D:"+File.separator+"text.html");
StringBuffer sb = new StringBuffer();
try {
java.net.URL url = new java.net.URL(domain);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream()));
Writer out=new FileWriter(f);
String line;
while ((line = in.readLine()) != null) {
line=new String(line.getBytes(),"UTF-8");
sb.append(line+"\n");
out.write(line+"\n\r");
}
System.out.println(sb.toString());
in.close();
} catch (Exception e) { // Report any errors that arise
sb.append(e.toString());
System.err.println(e);
System.err
.println("Usage: java HttpClient <URL> [<filename>]");
}
return sb.toString();
}
public static void main(String[] args) {
getWebCon("http://jwc.ncist.edu.cn/ClassTable/newPrintBid.jsp?Bid=计科B101");
}
}