我使用java io從可能輸出字符(如é)的服務器檢索文本。 然後使用System.err輸出它們,它們變成'?'。我正在使用UTF8編碼。怎麼了? int len = 0;閱讀unicode
char[] buffer = new char[1024];
OutputStream os = sock.getOutputStream();
InputStream is = sock.getInputStream();
os.write(query.getBytes("UTF8"));//iso8859_1"));
Reader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
do {
len = reader.read(buffer);
if (len > 0) {
if (outstring == null) {
outstring = new StringBuffer();
}
outstring.append(buffer, 0, len);
}
} while (len > 0);
System.err.println(outstring);
編輯:只是嘗試下面的代碼:
StringBuffer b = new StringBuffer();
for (char c = 'a'; c < 'd'; c++) {
b.append(c);
}
b.append('\u00a5'); // Japanese Yen symbol
b.append('\u01FC'); // Roman AE with acute accent
b.append('\u0391'); // GREEK Capital Alpha
b.append('\u03A9'); // GREEK Capital Omega
for (int i = 0; i < b.length(); i++) {
System.out.println("Character #" + i + " is " + b.charAt(i));
}
System.out.println("Accumulated characters are " + b);
出來是垃圾,以及:
Character #0 is a Character #1 is b Character #2 is c Character #3 is ¥ Character #4 is ? Character #5 is ? Character #6 is ? Accumulated characters are abc¥???
重新格式化的代碼;如果不正確請回復。 – trashgod 2010-04-20 04:57:19
儘管與這個問題無關,在這種用法中首選'StringBuilder'。 – trashgod 2010-04-20 05:10:47