Read any text file in Android and convert contents to UTF-8?
Is it possible to read a text file in Android and convert its content to UTF-8, so all the usual special characters are shown correctly? I am especially talking about some European characters, like Æ, Ø, Å, Ö, and Ä.
Thanks a lot.
Solution: I eventually used a mix of evilone's suggestion and some code that repla开发者_StackOverflowces the wrong characters with the correct ones. Not perfect, but it works.
Very robust example:
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);
EDIT:
How to auto-detect a file’s encoding
Your problem is automatic detection of charset. This is no trivial task. You should google around for existing solutions, like jChardet.
I eventually used a mix of evilone's suggestion and some code that replaces the wrong characters with the correct ones. Not perfect, but it works.
精彩评论