Android UTF-8编码不起作用? [英] Android UTF-8 encoding not working?

查看:151
本文介绍了Android UTF-8编码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在处理CSV文件。
在我的程序中,我正在使用OutputStreamWriter将数据写入csv文件。

I am working with a CSV file right now. In my program i am using an OutputStreamWriter to write data the csv file.

OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut, Charset.forName("UTF-8").newEncoder());

我尝试打印出此编写器的编码样式并得到以下内容:

I tried printing out the encoding style of this writer and get the following:

Log.i(TAG, "BODY ENCODING: " + myOutWriter.getEncoding());
Logcat: BODY ENCODING: UTF-8

但是当我尝试打开csv文件时在我的桌面上,它说该文件在Windows-1252中,因此我无法读取我需要的æøå字符。

But when i try to open the csv file on my desktop it says that the file is in windows-1252 so i cant read æøå chars which i need.

我在这里缺少明显的东西吗?或者我不明白outputStreamWriter的概念?我尝试了不同类型的编码,但似乎不起作用:)

Am i missing something obvious here or am i not understanding the concept of outputStreamWriter? I have tried different types of encoding, but it doesn't seem to work :)

当我尝试在Excel中打开时:

When i try to open in Excel:

推荐答案

您的文件实际上是UTF-8,而不是CP-1252。您的文本编辑器/查看器将其检测为CP-1251(因为没有多字节字符)。您可以通过在文件开头添加字节顺序标记(BOM)来帮助您的编辑器。即

Your file is actually UTF-8 not CP-1252. Your text editor/viewer detected it as CP-1251 (since no multi-byte characters). You can help your editor by adding byte order mark (BOM) in the beginning of the file. I.e.

static final byte[] UTF8_BOM = {0xEF,0xBB,0xBF};
...
fOut.write(UTF8_BOM);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut, Charset.forName("UTF-8").newEncoder());

这篇关于Android UTF-8编码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆