Java 如何在Android上读写UTF-8到磁盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2735323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read and write UTF-8 to disk on the Android?
提问by Rob Kent
I cannot read and write extended characters (French accented characters, for example) to a text file using the standard InputStreamReader methods shown in the Android API examples. When I read back the file using:
我无法使用 Android API 示例中显示的标准 InputStreamReader 方法在文本文件中读取和写入扩展字符(例如,法语重音字符)。当我使用以下方法读回文件时:
InputStreamReader tmp = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(tmp);
String str;
while ((str = reader.readLine()) != null) {
...
the string read is truncated at the extended characters instead of at the end-of-line. The second half of the string then comes on the next line. I'm assuming that I need to persist my data as UTF-8 but I cannot find any examples of that, and I'm new to Java.
读取的字符串在扩展字符处而不是行尾处被截断。然后字符串的后半部分出现在下一行。我假设我需要将我的数据保存为 UTF-8,但我找不到任何示例,而且我是 Java 新手。
Can anyone provide me with an example or a link to relevant documentation?
任何人都可以向我提供示例或相关文档的链接吗?
采纳答案by yanchenko
Very simple and straightforward. :)
非常简单明了。:)
String filePath = "/sdcard/utf8_file.txt";
String UTF8 = "utf8";
int BUFFER_SIZE = 8192;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), UTF8),BUFFER_SIZE);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), UTF8),BUFFER_SIZE);
回答by itsadok
When you instantiate the InputStreamReader
, use the constructor that takes a character set.
实例化 时InputStreamReader
,请使用采用字符集的构造函数。
InputStreamReader tmp = new InputStreamReader(in, "UTF-8");
And do a similar thing with OutputStreamWriter
并做类似的事情 OutputStreamWriter
I like to have a
我喜欢有一个
public static final Charset UTF8 = Charset.forName("UTF-8");
in some utility class in my code, so that I can call (see more in the Doc)
在我的代码中的某个实用程序类中,以便我可以调用(在Doc 中查看更多信息)
InputStreamReader tmp = new InputStreamReader(in, MyUtils.UTF8);
and not have to handle UnsupportedEncodingException
every single time.
并且不必UnsupportedEncodingException
每次都处理。
回答by Elliott Hughes
this should just work on Android, even without explicitly specifying UTF-8, because the default charset isUTF-8. if you can reproduce this problem, please raise a bug with a reproduceable test case here:
这应该只适用于 Android,即使没有明确指定 UTF-8,因为默认字符集是UTF-8。如果您可以重现此问题,请在此处提出带有可重现测试用例的错误:
回答by Mukund K Roy
if you face any such kind of problem try doing this. You have to Encode
and Decode
your data into Base64
. This worked for me. I can share the code if you need it.
如果您遇到任何此类问题,请尝试这样做。你必须Encode
和Decode
你的数据导入Base64
。这对我有用。如果你需要,我可以分享代码。
回答by William T. Mallard
Check the encoding of your file by right clicking it in the Project Explorer and selecting properties. If it's not the right encoding you'll need to re-enter your special characters after you change it, or at least that was my experience.
通过在项目资源管理器中右键单击文件并选择属性来检查文件的编码。如果它不是正确的编码,您需要在更改后重新输入特殊字符,或者至少这是我的经验。