java写文件,编码无法转换成isoiso 8859 1编码-1

I have a file which is encoded as iso-8859-1, and contains characters such as
I am reading this file with java code, something like:
File in = new File("myfile.csv");
InputStream fr = new FileInputStream(in);
byte[] buffer = new byte[4096];
while (true) {
int byteCount = fr.read(buffer, 0, buffer.length);
if (byteCount &= 0) {
String s = new String(buffer, 0, byteCount,"ISO-8859-1");
System.out.println(s);
However the ? character is always garbled, usually printing as a ? .
I have read around the subject (and learnt a little on the way)
but still can not get this working
Interestingly this works on my local pc (xp) but not on my linux box.
I have checked that my jdk supports the required charsets (they are standard, so this is no suprise) using :
System.out.println(java.nio.charset.Charset.availableCharsets());
解决方案 I suspect that either your file isn't actually encoded as ISO-8859-1, or System.out doesn't know how to print the character.
I recommend that to check for the first, you examine the relevant byte in the file. To check for the second, examine the relevant character in the string, printing it out with
System.out.println((int) s.getCharAt(index));
In both cases the result should be 244 0xf4 hex.
for general advice (the code presented is in C#, but it's easy to convert to Java, and the principles are the same).
In general, by the way, I'd wrap the stream with an InputStreamReader with the right encoding - it's easier than creating new strings "by hand". I realise this may just be demo code though.
EDIT: Here's a really easy way to prove whether or not the console will work:
System.out.println("Here's the character: \u00f4");
本文地址: &
我有一个文件,编码为iso-8859-1,并包含字符,如?。
我正在读这个文件与java代码, :
档案in = new File(“myfile.csv”);
InputStream fr = new FileInputStream(in);
byte [] buffer = new byte [4096];
while(true){ int byteCount = fr.read(buffer,0,buffer.length);
if(byteCount <= 0){ }
String s = new String(buffer,0,byteCount,“ISO-8859-1”);
System.out.println(s); }
但是?字符总是乱码,通常打印为? 。
我已经阅读过这个主题了。
但仍然无法正常工作
我已经检查我的jdk支持所需的字符集(他们是标准的,所以这是没有惊喜)使用: / p>
System.out.println(java.nio.charset.Charset.availableCharsets());
解决方案 我怀疑您的档案不是 编码为ISO-8859-1,或System.out不知道如何打印字符。
我建议您检查首先,检查文件中的相关字节。要检查第二个,检查字符串中的相关字符,使用打印出来。
System.out.println )s.getCharAt(index));
在这两种情况下,结果 0xf4 hex。
请参阅一般建议(代码提出在C#,但很容易转换为Java,原则是一样的)。
一般,顺便说一句,我会用一个 InputStreamReader 用合适的编码包装流 - 它比手动创建新字符串更容易。我知道这可能只是演示代码虽然。
编辑:这是一个很简单的方法来证明控制台是否工作:
System.out.println(“这里是字符:\\\?”);
本文地址: &
扫一扫关注官方微信java文件中文乱码,改字符集gbk、utf-8、ISO-8859-1都不好使【aide吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:26,363贴子:
java文件中文乱码,改字符集gbk、utf-8、ISO-8859-1都不好使收藏
我以前不是发过一个编码批量转换工具的么?输入文件夹路径,自动转换该路径下.java后缀文件的编码
电脑保存.java文件的时候选错编码模式了
去百度下载一个软件:920文本编辑器,就可以实现转码了。
登录百度帐号推荐应用iso-8859-1可以编码中文?新手求教...【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:627,439贴子:
iso-8859-1可以编码中文?新手求教...收藏
用jsp 发送get请求至servlet 其中pageEncoding=&ISO-8859-1&jsp代码:&form action=&testup&&
&input type=&text& name=&text1&&
&input type=&submit&&&/form&servlet代码:response.setContentType(&text/html&);PrintWriter out = response.getWriter();String str = request.getParameter(&text1&);out.print(str);text内输入“中文”二字,谷歌和火狐都能正常显示“中文”,ie显示乱码最不能理解的是三种浏览器编码都写的是iso-8859-1 iso什么时候能显示中文了...求高人指点...
浏览器很聪明。不过作为开发者你不应假设浏览器会替你自动转换。
post请求和get请求要进行转码
因为浏览器聪明,ie笨。
因为iso-8859-1不会处理未知字符(其实好像在编码范围内也没有未知字符),所以不会锟斤拷
改成GB2312试试
登录百度帐号推荐应用本帖子已过去太久远了,不再提供回复功能。}

我要回帖

更多关于 iso 8859 1编码 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信