import java.io.UnsupportedEncodingException;
public class test {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "hello world";
byte[] bytes = str.getBytes("UTF8");
System.out.println("bytes= "+bytes);
String srt2=new String(bytes,"UTF-8");
System.out.println("srt2= "+srt2);
}
}
运行结果:
String 转换成 byte[] :string.getBytes("UTF8") *string 指字符串
byte[] 转换成 String :new String(bytes,"UTF-8") *bytes 指字节数组