標籤:fixed ajp cu2 key hfs ddb utf-8 puts NPU
字元流出現的原因及思想
?由於位元組流操作中文不是特別方便,所以,java就提供了字元流。 ?字元流=位元組流+編碼錶。
編碼錶概述和常見的編碼錶 █ 編碼錶
?由字元及其對應的數值組成的一張表 █ 常見編碼錶
?ASCII/Unicode 字元集 ?ISO-8859-1 Latin-1 ?GB2312/GBK/GB18030 ?BIG5 ?UTF-8
| public class TestMain { /* * getBytes(String charsetName); 使用指定的字元集將此 String 編碼為 byte 序列,並將結果儲存到一個新的 byte 數組中。 String(byte[] bytes, String charsetName); 通過使用指定的 charset 解碼指定的 byte 數組,構造一個新的 String。 */ public static void main(String[] args) { String string = "hello world" ; byte[] bytes = string.getBytes(); //編碼 for(int i=0;i<bytes.length;i++){ System.out.print(bytes[i]); //輸出位元組碼 } //解碼 String jiemaStrin = new String(bytes); //通過使用平台的預設字元集解碼指定的 byte 數組,構造一個新的 String。//這個平台是作業系統,預設的編碼GBK System.out.print('\n'+jiemaStrin); } } |
public static void main(String[] args) throws UnsupportedEncodingException { String string = "hello world 梅浩" ; //編碼 byte[] bytes = string.getBytes("utf-8"); //參數不寫就是用系統預設GBK,但是我這被改成 utf-8 了; for(int i=0;i<bytes.length;i++){ System.out.print(bytes[i]); //輸出位元組碼 } //解碼 //String jiemaStrin = new String(bytes); //通過使用平台的預設字元集解碼指定的 byte 數組,構造一個新的 String。(我這裡已經把預設的改成utf-8了) String jiemaStrin = new String(bytes,"utf-8"); //這個平台是作業系統,預設的編碼GBK System.out.print('\n'+jiemaStrin); } } |
//unicode碼 :java 虛擬機器內部使用 String str = "梅浩"; //存字串常量區就是用 unicode 碼存的。
字元流概述(轉換流)
█ OutputStreamWriter 字元輸出資料流
?public OutputStreamWriter(OutputStream out); //建立使用預設字元編碼的 OutputStreamWriter。 ?public OutputStreamWriter(OutputStream out,String charsetName); █ InputStreamReader 字元輸入資料流
?public InputStreamReader(InputStream in); //建立一個使用預設字元集的 InputStreamReader。 ?public InputStreamReader(InputStream in,String charsetName); // 建立使用指定字元集的
Java——IO類 字元流概述