/* 编码:字符串转换成字节 解码:字节变字符串 String---->byte[] str.getBytes(); byte[]--->String; new String(byte[],charsetname); 编一次解一次 */ import java.util.*; import java.lang.Exception; class CodeDemo { public static void main(String[] args) throws Exception { String s = "你好"; byte[] b1 = s.getBytes(); System.out.println(Arrays.toString(b1)); String s1 = new String(b1,"ISO8859-1"); //CodeDemo.java:23: 错误: 未报告的异常错误UnsupportedEncodingException; 必须对其进行捕获或声明以便抛出 System.out.println("s1=" + s1); //加密 //对s1进行ISO8859-1编码 byte[] b2 = s1.getBytes("iso8859-1"); System.out.println(Arrays.toString(b2)); String s2 = new String(b2,"gbk"); System.out.println("s2="+ s2); } }