方法如下,正确编码打印不会乱码(不区分大小写)
private static void testEncode(String str) throws UnsupportedEncodingException{
System.out.println(str+"----------------");
System.out.println(new String(str.getBytes(), "GB2312"));
System.out.println(new String(str.getBytes(), "UTF-8"));
System.out.println(new String(str.getBytes(), "GBK"));
System.out.println(new String(str.getBytes(), "ISO8859_1"));
System.out.println(new String(str.getBytes(), "ASCII"));
System.out.println(new String(str.getBytes(), "GB18030"));
System.out.println(new String(str.getBytes(), "UTF-16"));
System.out.println(new String(str.getBytes(), "Unicode"));
}
20200606:优化:
private void testEncode(String str) throws UnsupportedEncodingException{ Set names=Charset.availableCharsets().keySet(); for (Iterator iter = names.iterator(); iter.hasNext();) { String charsetName = (String) iter.next(); if(Charset.isSupported(charsetName)&&new String(str.getBytes(), charsetName).contentEquals(str)){ System.out.println(charsetName); } } }
此种方法是获取系统所有支持的charset@参考文章