Java中,字串在不同編碼格式下所佔位元組數
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