我想如何从utf8格式的网站上获取内容,
我写了以下代码
try {
String webnames = "http://pathivu.com";
URL url = new URL(webnames);
URLConnection urlc = url.openConnection();
//BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
BufferedReader buffer = new BufferedReader(new InputStreamReader(urlc.getInputStream(), "UTF8"));
StringBuilder builder = new StringBuilder();
int byteRead;
while ((byteRead = buffer.read()) != -1)
builder.append((char) byteRead);
buffer.close();
String text=builder.toString();
System.out.println(text);
}
catch (IOException e)
{
e.printStackTrace();
}
但我无法获得正确的格式......
谢谢并提前......
答案 0 :(得分:3)
问题可能是您的控制台或System.out
不是UTF-8。
System.setOut(..)
您可能必须使用-Dfile.encoding=utf-8
或OutputStreamWriter
答案 1 :(得分:1)
您的代码看起来没问题......这里的问题是服务器中的数据不会是UTF-8格式..