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.
分享到:
相关推荐
struts 1
课程设计_基于java实现的论文选题系统源码程序+数据库+项目说明 如何使用本系统: 第一步:clone本项目到你的电脑上 第二步:将Essay_student项目导入到你的eclipse 第三步:将sql文件导入到你的mysql数据库中,推荐...
JAVA开发工具之idea 2021
Chapter 18: Final Submission and Essay
需要 Java 8 和 Maven 3 Wordnet(在数据/模型文件夹中)句子检测器、带有标签字典的 Maxent 模型和数据/模型中的解析器模型) 从下面的链接下载训练数据并放入 data/Answer_datasets 格式) 使用以下命令构建...
NULL 博文链接:https://essay.iteye.com/blog/342088
特性对比:| 客户端个数:I/O线程 | 1 : 1 | M : N(其中M可大于N) | M : 1(1个I/O线程处理多个客户端链接) | M : 0(不需
In the past few years there's been a ... In this essay I explore the reasons for agile methods, focusing not so much on their weight but on their adaptive nature and their people-first orientation.
[游记详情](/img/essay_detail.png) 游记列表 ![游记列表](/img/essay_list.png) 个人信息 ![个人信息](/img/perinfo_page.png) 搜索列表 ![搜素列表](/img/search_info.png) ## 系统设计 ### 邮箱验证 邮箱...
One after the other, potential rivals in the browser—Flash, Silverlight, and Java applets, to name a few—have come and (more or less) gone. Meanwhile, when a programmer named Ryan Dahl wanted to ...
Java platform experts Cade and Roberts provide expert guidance and background for designing effective J2EETM technology architectures—as well as comprehensive preparation for every exam element: ...