Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

Can anyone provide me with an example or a link to relevant documentation?

like image 405
Rob Kent Avatar asked Apr 29 '10 07:04

Rob Kent


People also ask

Does Android use UTF-8?

Android note: The Android platform default is always UTF-8. The StandardCharsets class defines constants for each of the standard charsets.

Is UTF-8 the same as UTF-8?

UTF-8 is a valid IANA character set name, whereas utf8 is not. It's not even a valid alias. it refers to an implementation-provided locale, where settings of language, territory, and codeset are implementation-defined.


1 Answers

When you instantiate the InputStreamReader, use the constructor that takes a character set.

InputStreamReader tmp = new InputStreamReader(in, "UTF-8");

And do a similar thing with 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)

InputStreamReader tmp = new InputStreamReader(in, MyUtils.UTF8);

and not have to handle UnsupportedEncodingException every single time.

like image 187
itsadok Avatar answered Oct 18 '22 19:10

itsadok