服务器接收字节数组作为输入流,然后用DataInputStream封装流。前2个字节表示字节数组的长度,第2个字节表示一个标志,下一个字节包含content.My问题是内容包含有2个字节的unicode字符。如何读取unicode字符?我的上一个代码是:Java如何读取套接字中的unicode字符?
DataInputStream dis = new DataInputStream(is);
int length = dis.readUnsignedShort();
int flag = dis.readUnsignedShort();
String content = "";
int c;
for (int i = 0; i < length - 4; i++) {
c = dis.read();
content += (char) c;
}
它只能读取ascII.thxs的帮助!
见BalusC答案这里:http://stackoverflow.com/questions/4505057/datainputstream-and-utf-8 –
又见[ readChar](http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readChar())方法,如果这些代码点是big-endian UTF-16。 – McDowell
thx太多了。答案解决了我的问题。 –