如何读取和写入UTF-8到磁盘上的Android? [英] How to read and write UTF-8 to disk on the Android?
问题描述
我无法读取和写入扩展字符使用在Android API的例子所示的标准InputStreamReader的方法(法语重音字符,例如)到一个文本文件中。当我读回文件中使用:
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:
InputStreamReader tmp = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(tmp);
String str;
while ((str = reader.readLine()) != null) {
...
读取的字符串被截断在扩展字符,而不是在结束的线。串的第二半然后是下一行。我假设我要坚持我的数据为UTF-8,但我找不到任何例子,我是新来的Java。
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.
任何人都可以给我一个例子或链接到相关的文档?
Can anyone provide me with an example or a link to relevant documentation?
推荐答案
非常简单明了。 :)
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);
这篇关于如何读取和写入UTF-8到磁盘上的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!