package lianxi; import java.io.UnsupportedEncodingException; /** * */ public class one { public static void main(String[] args) throws UnsupportedEncodingException { //获取main方法入口下的文件编码格式 System.out.println(System.getProperty("file.encoding"));//输出UTF-8 String s = "abc你好么"; byte[] bytes1 = s.getBytes("Unicode"); System.out.println(bytes1.length); // 14 在unicode编码中每个字符占2个字节,但有些是4字节,具体怎么算的,未知 byte[] bytes3 = s.getBytes("UTF-8"); System.out.println(bytes3.length); // 12 = 1+1+1+3+3+3 byte[] bytes4 = s.getBytes("GBK"); System.out.println(bytes4.length); // 9 = 1+1+1+2+2+2 byte[] bytes2 = s.getBytes();//默认为项目文件编码:UTF-8 System.out.println(bytes2.length); // 12 = 1+1+1+3+3+3 }}
字符串在不同编码下的字节数,参考地址:http://blog.csdn.net/cuiyaoqiang/article/details/52056175
System.getProperty();参考地址: http://blog.csdn.net/cuiyaoqiang/article/details/52056175
file.encoding 参考地址: http://blog.csdn.net/loongshawn/article/details/50918506
Java中,字符串在不同编码格式下所占字节数
最新推荐文章于 2021-02-20 21:02:18 发布