1
我注意到我的Android應用程序的事件日誌中出現UTFDataFormatException錯誤。我正在閱讀的文件是由我的應用程序編寫的,但它存儲有關用戶故事的信息,它可能包含任何類型的字符/字符串。我想知道讀/寫某些字符是否有問題?Android從UTF文本文件讀取UTFDataFormatException錯誤
我寫的使用:
dos.writeUTF(myJSONString);
,我在使用閱讀:
textJSONString = dis.readUTF();
一些我所記錄的錯誤堆棧跟蹤的是:
class: class java.io.UTFDataFormatException
message: bad second or third byte at 1795
java.io.charset.ModifiedUtf8.decode(ModifiedUtf8.java:53)
java.io.DataInputStream.decodeUTF(DataInputStream.java:444)
java.io.DataInputStream.decodeUTF(DataInputStream.java:438)
java.io.DataInputStream.readUTF(DataInputStream.java:433)...
我已經查找解碼方法的來源,但我不明白髮生了什麼/爲什麼它失敗:
public static String decode(byte[] in, char[] out, int offset, int utfSize) throws UTFDataFormatException {
int count = 0, s = 0, a;
while (count < utfSize) {
if ((out[s] = (char) in[offset + count++]) < '\u0080') {
s++;
} else if (((a = out[s]) & 0xe0) == 0xc0) {
if (count >= utfSize) {
throw new UTFDataFormatException("bad second byte at " + count);
}
int b = in[offset + count++];
if ((b & 0xC0) != 0x80) {
throw new UTFDataFormatException("bad second byte at " + (count - 1));
}
out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
} else if ((a & 0xf0) == 0xe0) {
if (count + 1 >= utfSize) {
throw new UTFDataFormatException("bad third byte at " + (count + 1));
}
int b = in[offset + count++];
int c = in[offset + count++];
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
throw new UTFDataFormatException("bad second or third byte at " + (count - 2));
}
out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
} else {
throw new UTFDataFormatException("bad byte at " + (count - 1));
}
}
return new String(out, 0, s);
}
任何想法?
它適用於簡單文件嗎?當你用文本編輯器打開文件時,你會發現什麼奇怪的東西嗎? – Caner