Java IO流(2)-字符集

編碼
變長Unicode字符

UTF-8
字母 1個字節
漢字 3個字節

public class Encode {
    public static void main(String[] args) {
        String src="這是我的abc";
        byte[] bytes = src.getBytes();
        System.out.println(bytes.length);
    }
}

在這裏插入圖片描述

GBK
字母 1個字節
漢字 2個字節

import java.io.UnsupportedEncodingException;

public class Encode {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String src="這是我的abc";
        byte[] bytes = src.getBytes();


        bytes = src.getBytes("GBK");
        System.out.println(bytes.length);
    }
}

在這裏插入圖片描述
解碼

import java.io.UnsupportedEncodingException;

public class Encode {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String src="這是我的abc";
        byte[] bytes = src.getBytes();

        String s;
        //解碼
        s=new String(bytes,0,bytes.length,"utf-8");
        System.out.println(s);
    }
}

可能會遇到的問題

import java.io.UnsupportedEncodingException;

public class Encode {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String src="這是我的abc";
        byte[] bytes = src.getBytes();

        String s;
        //解碼
        //字符集不統一
        s=new String(bytes,0,bytes.length,"gbk");
        System.out.println(s);

        //字節數不夠
        s=new String(bytes,0,bytes.length-1,"utf-8");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章