java中文件的编码方式主要就是那么几种,那么我们来看一下区别
package IO输入输出流;
public class EncodeDemo {
public static void main(String[] args)throws Exception {
String s="高维远ABC";
byte[] byte1=s.getBytes();
for (byte b:byte1)
{
//把字节换成int的方式进行显示
System.out.print(Integer.toHexString(b &0xff)+" ");
}
System.out.println();
//在gbk中,中文占用两个字节,英文占用一个字节
byte[] byte2=s.getBytes("gbk");
for (byte b:byte2)
{
//把字节换成int的方式进行显示
System.out.print(Integer.toHexString(b &0xff)+" ");
}
System.out.println();
//utf-8中,中文占用3个字节,英文占用1个字节
byte[] byte3=s.getBytes("utf-8");
for (byte b:byte3)
{
//把字节换成int的方式进行显示
System.out.print(Integer.toHexString(b &0xff)+" ");
}
System.out.println();
//java是utf-16be编码,一个字符占用两个字节
byte[] byte4=s.getBytes("utf-16be");
for (byte b:byte1)
{
//把字节换成int的方式进行显示
System.out.print(Integer.toHexString(b &0xff)+" ");
}
/**
* 当你的某一个序列想变成字符串的时候
* 一定要用这种编码形式进行转换在进行
* 转变,不然会出现乱码情况
*/
String str1=new String(byte4);
System.out.println(str1);
String str2=new String(byte4,"utf-16be");
System.out.println(str2);
}
}