今天在開發(fā)關(guān)于字符流轉(zhuǎn)換的時(shí)候遇到了這個(gè)問(wèn)題,查詢了一些相關(guān)資料,得到解決辦法如下:
Charset charset = Charset.forName("UTF8");
ByteBuffer bb = charset.encode("Hello world!");
byte[] b = bb.array();
for (int i = 0; i < b.length; i++) {
byte c = b[i];
System.out.println(c);
}
CharBuffer cb = charset.decode(bb);
System.out.println(cb.toString());