简体   繁体   English

如何在Android上读写UTF-8到磁盘?

[英]How to read and write UTF-8 to disk on the Android?

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. 我无法使用Android API示例中显示的标准InputStreamReader方法将扩展字符(例如法语重音字符)读写到文本文件中。 When I read back the file using: 当我使用以下方式回读文件时:

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? 任何人都可以向我提供相关文档的示例或链接吗?

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);

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

this should just work on Android, even without explicitly specifying UTF-8, because the default charset is UTF-8. 这应该只适用于Android,即使没有明确指定UTF-8,因为默认的字符集 UTF-8。 if you can reproduce this problem, please raise a bug with a reproduceable test case here: 如果你能重现这个问题,请在这里提出一个可重现的测试用例的bug:

http://code.google.com/p/android/issues/entry http://code.google.com/p/android/issues/entry

if you face any such kind of problem try doing this. 如果您遇到任何此类问题,请尝试这样做。 You have to Encode and Decode your data into Base64 . 您必须将数据EncodeDecodeBase64 This worked for me. 这对我有用。 I can share the code if you need it. 如果需要,我可以共享代码。

Check the encoding of your file by right clicking it in the Project Explorer and selecting properties. 在Project Explorer中右键单击文件并选择属性,检查文件的编码。 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. 如果它不是正确的编码,您需要在更改后重新输入特殊字符,或者至少这是我的经验。

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

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