乱码总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.UnsupportedEncodingException;


* @description 类描述:
* @author 作者:LIYONG
* @create 创建时间:2016-10-25
* @update 修改时间:2016-10-25
*/

public class {

public static void main(String[] args) throws UnsupportedEncodingException {
String defaultStr = new String("my english name is lee,my age is 0,我热爱我的工作编程,123");
method1(defaultStr);
method2(defaultStr);
method3(defaultStr);
}

public static void method1(String defaultStr) throws UnsupportedEncodingException {
byte[] encodeByUtf8 = defaultStr.getBytes("UTF-8");
System.out.println();
System.out.println("===========method1 encodeByUtf8 " + defaultStr + "==============");
cmmonMethod(encodeByUtf8);

}

public static void method2(String defaultStr) throws UnsupportedEncodingException {
byte[] encodeByGbk = defaultStr.getBytes("GBK");
System.out.println();
System.out.println("===========method2 encodeByGbk " + defaultStr + "==============");
cmmonMethod(encodeByGbk);
}

public static void method3(String defaultStr) throws UnsupportedEncodingException {
byte[] encodeiso88591 = defaultStr.getBytes("ISO8859-1");
System.out.println();
System.out.println("===========method3 encodeiso88591 " + defaultStr + "==============");
cmmonMethod(encodeiso88591);
}

private static void cmmonMethod(byte[] bytes) throws UnsupportedEncodingException {
String utf8 = new String(bytes, "UTF-8");
System.out.println("utf8 :" + utf8);
String utf16 = new String(bytes, "UTF-16");
System.out.println("utf16 :" + utf16);
String gbk = new String(bytes, "GBK");
System.out.println("gbk :" + gbk);
String iso88591 = new String(bytes, "ISO8859-1");
System.out.println("iso88591 :" + iso88591);
}

}

结果: