Firstable我想说我试图谷歌那个问题并在stackoverflow上搜索anwser,我知道Java将String存储为UTF-16。我将使用ISO格式编码的String转换为UTF-8时遇到问题。我正在下载的网站在ISO中显示字符,我的程序的其余部分也将字符串转换为流使用UTF-8编码。
如何将inputHTML字符串的编码更改为UTF-8? 我试图用Writer来操纵它:
OutputStream os = new ByteArrayOutputStream();
Writer wr = new OutputStreamWriter(os, "UTF-8");
Writer writer = new BufferedWriter(wr);
writer.write(inputHTML);
writer.close();
但不知道如何将OutputStream更改为我转换后的新String。 这是我的代码:
URL url = new URL("http://www.onet.pl");
InputStream is = url.openStream();
Reader reader = new InputStreamReader(is, "ISO-8859-2");
StringWriter writer = new StringWriter();
char[] buf = new char[4096];
int len;
while ((len = reader.read(buf)) >= 0)
writer.write(buf, 0, len);
StringBuffer sb = writer.getBuffer();
String inputHTML = new String(sb);
答案 0 :(得分:2)
你没有。您将其写入使用适当编码初始化的编写器,编写器将在写出时将其转换。