测试环境编码GBK
代码:
public class CharsetTest {
public static void main(String[] args) throws Exception{
String str = "中国123";
byte[] bytes = str.getBytes("unicode");
str = new String(bytes,"unicode");
System.out.println("unicode str:"+str);
bytes = str.getBytes("UTF-8");
str = new String(bytes,"UTF-8");
System.out.println("UTF-8 str:"+str);
}
}
输出:
unicode str:中国123
UTF-8 str:中国123
总结:java采用unicode编码,也即jvm本身采用的数据编码为Unicode,所以在环境不统一的情况下,可以先获得当前内容的unicode编码,然后再进行转换,则不会出现乱码