在Java中如何进行文件的读写十分重要,Java IO流是必备的知识点。在学习Java IO流之前必须了解关于编码的那些事。
看代码:
public class EncodeDemo {
public static void main(String[] args) throws Exception{
String s = "我爱Code";
// 使用项目默认的编码gbk
byte[] bytes1 = s.getBytes();
System.out.print("项目默认编码:");
for(byte b:bytes1) {
// 把字节转换成int类型,以16进制显示,&0xff是为了把前面的24个0去掉
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
// 使用gbk编码
byte[] bytes2 = s.getBytes("gbk");
System.out.print("gbk:");
for(byte b:bytes2) {
// 在gbk编码下,一个中文占两个字节,英文占一个字节
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
// 使用utf-8编码
byte[] bytes3 = s.getBytes("utf-8");
System.out.print("utf-8:");
for(byte b:bytes3) {
// 在utf-8编码下,中文占三个字节,英文占一个字节
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
// java是双字节编码utf-16be
byte[] bytes4 = s.getBytes("utf-16be");
System.out.print("utf-16be:");
for(byte b:bytes4) {
// 在utf-16be编码下,中文占两个字节,英文占两个字节
System.out.print(Integer.toHexString(b & 0xff) + " ");
}
System.out.println();
/**
* 把字节序列变成字符串时,应保持和原来的编码格式相同,否则是乱码
*/
String ss = new String(bytes4); //使用项目默认的编码
System.out.println(ss);
String sss = new String(bytes4, "utf-16be"); //使用utf-16be编码
System.out.println(sss);
}
}
输出:
项目默认编码:ce d2 b0 ae 43 6f 64 65
gbk:ce d2 b0 ae 43 6f 64 65
utf-8:e6 88 91 e7 88 b1 43 6f 64 65
utf-16be:62 11 72 31 0 43 0 6f 0 64 0 65
br1
我爱Code
从结果可以看出,
在项目默认编码下(eclipse中默认编码是gbk),中文占两个字节,英文占一个字节;gbk编码下,中文占两个字节,英文占一个字节;utf-8编码下,中文占三个字节,英文一个字节;utf-16be编码下,中文英文均占两个字节。
同时要注意的是,把字节序列转换成字符串时,编码应保持相同,不然会出现乱码。