这个方法可以查看我们当前运行时默认的字符编码:
String defaultCharsetName=Charset.defaultCharset().displayName();
String encoding = System.getProperty("file.encoding");
在字符串转 字节数组时指定字符集:
"中文".getBytes("GBK");
影响字符编码的设置有:
1、java运行的平台,没有做任何设置的情况下java取的是平台的编码方式, 中文windows一般为 GBK, linux一般为UTF-8.
2、运行时设置:java -Dfile.encoding=utf-8
3、在程序里面设置:
1)
Properties pps=System. getProperties();
pps.put("file.encoding","UTF-8");
System.setProperties(pps);
2)
System.setProperty("file.encoding","UTF-8");
将字符串转为节数组时尽量指定一下编码方式,不同平台下所默认的编码方式是不一样的,所取得的节数组就会不同(一个汉字:GBK:2byte, UTF-8:3),导致接下来的处理逻辑错误。