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.

你可能感兴趣的:(java,jvm,swing,OS,F#)