Charset charset = StandardCharsets.UTF_8;
String str = "字符串----------------abc";
ByteBuffer buffer = ByteBuffer.allocate(1024);
//将字符串转成ByteBuffer
buffer.put(str.getBytes(charset));
//切换读取数据的模式
buffer.flip();
//----将ByteBuffer转成字符串1------------------------------------------------------------------------------
String string1 = charset.decode(buffer).toString();
System.out.println(string1);
//----将ByteBuffer转成字符串2------------------------------------------------------------------------------
buffer.flip();
String string2 = new String(buffer.array(), 0, buffer.limit());
System.out.println(string2);
注意:本文归作者所有,未经作者允许,不得转载