views:

1019

answers:

3

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?

+4  A: 

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

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

and not have to handle UnsupportedEncodingException every single time.

itsadok
+3  A: 

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);
alex
Thanks. Does that work when saving to the private data area as well?
Rob Kent
Sure. Nothing Android-specific here above `openFileOutput(..)`.
alex
I mean using openFileOutput?
Rob Kent
Yes. You can replace generic `new FileOutputStream(filePath)` with Android's `openFileOutput(..)`.
alex
Great - thanks.
Rob Kent
A: 

this should just work on Android, even without explicitly specifying UTF-8, because the default charset is UTF-8. if you can reproduce this problem, please raise a bug with a reproduceable test case here:

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

Elliott Hughes
I definitely had to change to UTF-8 to fix the bug. I had a postive result with my users who previously found that 'extended characters', such as French E circumflex, caused the file not to reread properly. I got a user to send me his file and when I read it back in the debugger I saw that the extended characters were being treated as a line-ending. I still have the original files. When I am back at home I will run up a test project using them to demonstrate. See my first question for code: http://stackoverflow.com/questions/2700983/problem-saving-file-on-motorola-droid-android-2-1
Rob Kent