Java essay

        Havn't try java for severals months,I try to write a notepad to review something about Swing and IO.At first,I thought it's just a piece of cake,but lately I found it's not so easy,especially if I want to make it more functional.When open a text file,I found I can't read the Chinese,they are all disorderly code when displayed.

  At first,I use BufferedReader in=new BufferedReader(new FileReader(file)) to read the file,but it's error in displaying Chinese.Then I begin to use BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8")),but it also fails.I got confused,then I change the "utf-8" to "gbk" and it successed.But I don't know why.

  I read others' articles about java char coding,I know that jvm's default coding is based on the environment of the os and language.If the"gbk" can work,does it mean that the text file's char coding is "gbk"? I still can't get it.

  Here are some good links:

               http://www.iteye.com/topic/108540   

               http://www.iteye.com/topic/311583 

 

 After reading that,I wrote a piece of code:

       File file=new File("G:\1.txt");

       try{

                FileInputStream in=new java.io.FileInputStream(file);

                byte[] b=new byte[3];

                in.read(b);

                in.close();

                if(b[0]==-17&&b[1]==-69&&b[2]==-65)

                   System.out.println(f.getName()+"it's UTF-8");

                else System.out.println(f.getName()+"maybe GBK");

            }catch(Exception e){

                 e.printStackTrace();

              }

   Then I know the text file's char coding is not "utf-8"(I think it's gbk),and I use

Charset.defaultCharet() know that the jvm default charset is "utf-8".then I know what happened.

  There are some open source projects about how to define the char coding exactly,like the cpdetector(http://cpdetector.sourceforge.net/), it's very helpful.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值