编码
1,使用默认方式(UTF-8)进行编码
public byte[] getBytes()
//public byte[] getBytes()
String str ="abc";
byte[] bytes1 =str.getBytes();
2,使用指定方式进行编码
public byte[] getBytes(String charsetName)
byte[] bytes2 = str.getBytes("GBK");
因为可能存在声明的编码方式不存在,所以需要抛出异常UnsupportedEncodingException
解码
1,使用默认方式(UTF-8)进行解码
String(byte[] bytes)
String str =new String(bytes);
2,使用指定方式 进行解码
String(byte[] bytes, String charsetName)
String str = new String(bytes,"GBK");