java 字符编码 ASCII GBK UTF-8简单知识了解

刚打开IDE的你不知道有没有遇到过下述字符乱码现象。。。

想要解决上述问题,那就跟我一起探个究竟吧

字符编码(1)

字符编码
字符:0,a,我,①,の,……
计算机只用0和1, 1 bit(0 或者 1)
ASCII码
(American Standard Code for Information Interchange)
美国信息交换标准代码,奠定计算机编码基础
用一个字节(1 Byte=8 bits) 来存储a-z,A-Z,0-9和一些常用符号
用于显示英语及西欧语言
回车键(13, 00001101), 0(48, 00110000), A(65,01000001), a(97, 01100001)
 

字符编码(2)

字符编码
ASCII编码采用1 Byte,8 bits,最多256个字符
ASCII无法适应其他地方,如汉字数量有十几万
扩展编码( 加字节 )
ISO8859(1-15) 西欧语言
GB2132, GBK,GB18030 ASCII+中文
Big5 ASCII + 繁体中文
Shift_JIS ASCII+日文
……
Unicode 编码
 
 

字符编码(3)

中文编码
GB2312,1980年发布,7445个字符(6763个简体字),包括拉丁字
母、希腊字母、日文平假名及片假名字母、俄语西里尔字母等
682个符号
GBK,1995年发布,21886 个汉字和符号,包括GB2312和Big 5
GB18030(2000, 2005两个版本),70244个汉字和符号,包括GBK和
GB2312
Big 5,繁体中文
GB18030 > GBK > GB2312
 
 

字符编码(4)

Unicode(字符集)
目标:不断扩充,存储全世界所有的字符
编码方案
UTF-8,兼容ASCII,变长(1-4个字节存储字符),经济,方便传输
UTF-16,用 变长(2-4个字节)来存储所有字符
UTF-32,用32bits(4个字节)存储所有字符
 
 

字符编码(5)

ANSI编码
Windows上非Unicode的默认编码(Windows code pages)
在简体中文Windows操作系统中,ANSI 编码代表 GBK 编码
在繁体中文Windows操作系统中,ANSI编码代表Big5
记事本默认是采用ANSI保存
ANSI编码文件不能在兼容使用
 
 

Java的字符编码

源文件编码:采用UTF-8编码

程序内部采用UTF-16编码存储所有字符(不是程序员控制)
 
和外界(文本文件)的输入输出尽量采用UTF-8编码
不能使用一种编码写入,换另外一种编码读取
 
通过CharsetTest.java, TextTestUTF8.java来
了解Java的字符编码

 

CharsetTest.java

public class CharsetTest {
    public static void main(String[] args) {
        //默认字符集为UTF-8
        Charset c= Charset.defaultCharset();
        System.out.println(c.getClass().getName());
        //获得java支持的所有字符集
        SortedMap<String,Charset>sm=Charset.availableCharsets();//SortedMap is a Map that further provides a total ordering on its keys
        Set<String>keySet=sm.keySet();

        for(String each:keySet)
            System.out.println(each);

    }
}

输出结果:

可以看出默认字符集是UTF-8,这里的全部可用字符集只截图了一部分。

TextTestUTF8.java

public class TextTestUTF8 {
    public static void writeFile(String filePath){
        FileOutputStream fos=null ;//直接怼在文件上,节点类
        OutputStreamWriter osw=null;//转化类
        BufferedWriter bw=null;//装饰类
        try {
            fos=new FileOutputStream(filePath);
            osw=new OutputStreamWriter(fos);
            bw=new BufferedWriter(osw);
            bw.write("Hello World");
            bw.newLine();
            bw.write("~123@");
            System.out.println("写入完成!");

        } catch (Exception e) {
            e.printStackTrace();
        }
finally {
            try {
                bw.close();//关闭最后一个类,会将所有的底层流都关闭
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public static void readFile(String filePath)  {
        FileInputStream fis=null;
        InputStreamReader isr=null;
        BufferedReader br=null;
        String line;
        try{
            fis=new FileInputStream(filePath);
            isr=new InputStreamReader(fis);
            br=new BufferedReader(isr);
            System.out.println("读取结果:");
            while ((line=br.readLine())!=null){
                System.out.println(line);
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }

        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        String filePath="E:\\ideaPro\\testNew\\src\\CharSet\\test";
        writeFile(filePath);
        readFile(filePath);

    }
}

Console输出结果:

测试文件截图:(位于src包下面的CharSet子包中名为test的.txt文件)

 

注:

转载需注明出处

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值