作者LPH66 (-858993460)
看板java
标题Re: [问题] 读档,简体字变乱码
时间Sat May 14 06:42:40 2011
※ 引述《ta0306556 (POIL)》之铭言:
: File f=new File("testsent.txt");
: FileInputStream fr = new FileInputStream(f);
: BufferedReader br = new BufferedReader( new InputStreamReader( fr,"UTF-8" ) );
: String post ="";
: while((post = br.readLine())!= null)
: {
: System.out.println(post);
: }
: 在程式中,我已经设定编码为'utf-8'
: 然後我将文件存成 utf-8 (无BOM)
: 大部分的简体字都可以正确读出。
: 但遇到某些特定字元就会出问题。
: 例:"2000年 奥运 在 雪梨 举办"
: 输出:"2000年 ?? 在 雪梨 ??"
: 从例子中可以很明显的看出,奥运跟举办都变成"?"了。
: 当然还有其他的字也是这样。
: 请问各位大大,这种问题该怎麽解决呢?
: 我已经查GOOGLE,一整天了。依然不知道怎麽解。所以到此一问。
: 感谢大家。
我会猜这不是读取的问题 而是 System.out.println 的转码问题
因为你印到了 System.out 去 在印出时会做转码 (转成你的 console 用的编码)
所以转不过去的就变问号了
你可以试着用 .charAt() 去查询读进来的字串里每个字的字码
如果没有出现 ? (0x3F, 63) 或 U+FFFD (0xFFFD, 65533) 应该就是读取成功了
--
'Oh, Harry, don't you
see?' Hermione breathed. 'If she could have done
one thing to make
absolutely sure that every single person in this school
will read your interview, it was
banning it!'
---'Harry Potter and the order of the phoenix', P513
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.30.135
1F:推 ta0306556:好像真的是如此。感谢大大。 05/14 06:57