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);
}
}