如何将UTF-8字符串插入到RecordStore中并将其作为UTF-8字符串读取?
谢谢
发布于 2011-06-25 21:58:38
//write
ByteArrayOutputStream boStream = new ByteArrayOutputStream();
DataOutputStream doStream = new DataOutputStream(boStream);
doStream.writeUTF(myString);
temp.addRecord(boStream.toByteArray(), 0, boStream.size());//read
ByteArrayInputStream biStream = new ByteArrayInputStream(temp.getRecord(id));
DataInputStream diStream = new DataInputStream(biStream);
myString = diStream.readUTF();发布于 2011-06-25 05:03:25
我把这个问题的答案弄错了。RecordStore仍然存储字节数组。您需要做的是将其转换为字节数组,然后再转换回来。只需使用string.getBytes(),然后像这样存储它,然后与之相反的是String str = new String(bytes);。希望这能有所帮助。J2ME或J2SE的默认字符集都是UTF-8,所以没有什么问题。
https://stackoverflow.com/questions/6473495
复制相似问题