IO(编码与解码Demo)

编码:字符串变成字节数组。
String -->byte[]; str.getBytes(charsetName);

解码:字节数组变成字符串。
byte[] -->String; new String(byte[],charsetName);

import java.util.*;
class EncodeDemo 
{
    public static void main(String[] args) throws Exception
    {
        String s = "小萝莉漫许可证";

        byte[] b1 = s.getBytes("GBK");
        System.out.println(Arrays.toString(b1));
        String s1 = new String(b1,"iso8859-1");
        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);       
    }
}

你可能感兴趣的:(IO(编码与解码Demo))