java 编码类型

来源:互联网 发布:mac 搭建代理服务器 编辑:程序博客网 时间:2024/06/05 00:28

编码类型

ASCII:美国标准信息交换码。
           用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表。
          用一个字节的8为表示。
GB2312:中国的中文编码表。
           用俩个字节表示一个汉字。
GBK:扩容后的中国中文编码表。融合了更多的中文字符。大约2万个。
        
Unicode:国际标准码,融合了多种文字(java的默认编码方式)。 
             所有文字都用俩个文字来表示。
UTF-8:最多用三个字节来表示一个字符。最通用的码表。

java中的流中编码的转化例子

import java.io.*;public class EncodeStrem {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stub              //writeText();              readText();}public static void readText() throws IOException{InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");char[]  buf = new char[6];isr.read(buf);String s=new String(buf);System.out.println(s);}     public static void writeText() throws IOException     {     OutputStreamWriter osw=      new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8");           osw.write("你好");           osw.close();     }}

java中编码解码的实例

import java.io.*;import java.util.*;public class EncodeDemo {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stub               String s= "你好";               byte[]  b= s.getBytes("utf-8");//编码过程            // String  s1=  new String(b,"gbk");//解码过程              // System.out.println(s1);               //System.out.println(Arrays.toString(b));                                            String s3 = new String(b,"ISO8859-1");//进行了错误的解码后               System.out.println(s3);               byte[] b2 = s3.getBytes("ISO8859-1");//进行一次员解码的编码得到原来的字节                              System.out.println(Arrays.toString(b2));               String s2 = new String(b2,"utf-8");               System.out.println(s2);               //注意:如果用UTF-8是不适用这种方法的}   }



0 0