简体   繁体   English

使用utf-8的opencsv CSVWriter似乎不适用于多种语言

[英]opencsv CSVWriter using utf-8 doesn't seem to work for multiple languages

I have a very annoying encoding problem using opencsv. 我使用opencsv有一个非常烦人的编码问题。 When I export a csv file, I set character type as 'UTF-8'. 导出csv文件时,我将字符类型设置为'UTF-8'。

CSVWriter writer = new CSVWriter(new OutputStreamWriter("D:/test.csv", "UTF-8"));

but when I open the csv file with Microsoft Office Excel 2007, it turns out that it has 'UTF-8 BOM' encoding? 但是当我使用Microsoft Office Excel 2007打开csv文件时,结果证明它具有“ UTF-8 BOM”编码?

Once I save the file in Notepad and re-open, the file turns back to UTF-8 and all the letters in it appears fine. 将文件保存在记事本中并重新打开后,文件将恢复为UTF-8,并且其中的所有字母看起来都很好。 I think I've searched enough, but I haven't found any solution to prevent my file from turning into 'UTF-8 BOM'. 我想我已经搜索了足够多的东西,但是还没有找到任何解决方案来防止我的文件变成“ UTF-8 BOM”。 any ideas, please? 有什么想法吗?

I suppose your file has a 'UTF-8 without BOM' encoding. 我想您的文件具有“没有BOM的UTF-8”编码。 You better feed BOM encoding to your file, even though it's not necessary in most cases, but only one obvious exception is when you deal with ms excel. 您最好将BOM编码输入文件,即使在大多数情况下不是必需的,但是处理ms excel时只有一个明显的例外。

FileOutputStream os = new FileOutputStream(file);
os.write(0xef);
os.write(0xbb);
os.write(0xbf);
CSVWriter csvWrite = new CSVWriter(new OutputStreamWriter(os));

Now your file will be understood by excel as utf-8 csv. 现在,您的文件将被excel理解为utf-8 csv。

UTF-8 and UTF-8 Signature (which incorrectly named sometimes as UTF-8 BOM ) are same encodings, and signature is used only to distinguish it from any other encodings . UTF-8UTF-8 Signature (有时错误地称为UTF-8 BOM )是相同的编码,并且签名仅用于将其与任何其他编码区分开 Any unicode application should process UTF-8 signature (which is three bytes sequence EF BB BF ) correctly. 任何Unicode应用程序都应正确处理UTF-8签名(三个字节序列EF BB BF )。

Why Java is specifically adds this signature and how to stop it doing that I don't know. 我不知道为什么Java是专门添加此签名的,以及如何阻止它签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM