编码格式检测
代码如下:
public static void main(String[] args) throws UnsupportedEncodingException {
//将要测试的字符串写入test中,直接运行即可
String test= "ÖÐÎÄ";
System.out.println(test);
System.out.println(test.getBytes());
System.out.println(test.getBytes("GB2312"));
System.out.println(test.getBytes("ISO8859_1"));
System.out.println(new String(test.getBytes()));
System.out.println(new String(test.getBytes(), "GB2312"));
System.out.println(new String(test.getBytes(), "ISO8859_1"));
System.out.println(new String(test.getBytes("GB2312")));
System.out.println(new String(test.getBytes("GB2312"), "GB2312"));
System.out.println(new String(test.getBytes("GB2312"), "ISO8859_1"));
System.out.println(new String(test.getBytes("ISO8859_1")));
System.out.println(new String(test.getBytes("ISO8859_1"), "GB2312"));
System.out.println(new String(test.getBytes("ISO8859_1"), "ISO8859_1"));
System.out.println(new String(test.getBytes("GB2312"), "ISO8859_1"));
}
运行结果如下:
ÖÐÎÄ
[B@15db9742
[B@6d06d69c
[B@7852e922
ÖÐÎÄ
����
ÖÃÎÄ
????
????
????
����
中文
ÖÐÎÄ
????
附:FTP文件名的转化。解决中文乱码问题
//可读格式转为文件名格式
new String("中文".getBytes("GB2312"), "ISO8859_1");
//文件名转为可读格式
new String("ÖÐÎÄ".getBytes("ISO8859_1"), "GB2312");