简体   繁体   中英

Android read file encoding issue

I'm trying to read a file from the SD card and I've been told it's in unicode format. However, when I try to read the file I get the following:

编码文件

This is the code I'm using to read the file:

InputStreamReader fw = new InputStreamReader(new FileInputStream(root.getAbsolutePath()+"/Drive/sdk/cmd.62.out"), "UTF-8");
char[] buf = new char[255];     
fw.read(buf);
String readString = new String(buf);
Log.d("courierread",readString);    
fw.close();

If I write that output to a file this is what I get when I open it in a hex editor: 十六进制信息

Any thoughts on what I need to do to read the file correctly?

Does the file have a byte-order mark ? In that case look at Reading UTF-8 - BOM marker

EDIT (from comment) : That looks like little-endian UTF-16 to me. Try the charset "UTF-16LE".

The file you show in the hex editor is not UTF-8 encoded , it looks more like UTF-16. This means you must specify UTF-16 as the encoding in your code (probably the UTF-16LE variant).

If it were UTF-8 encoded, then it would represent all characters representable in ASCII using just a single byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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