writeUTF() 和 readUTF() 分别是DataOutputStream 和 DataInputStream对象的的方法,他们提供了一个由从Unicode到UTF-8的相互转化的途径。
仔细看看midp的说明文档,可以看到以下内容
writeUTF() :
First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for the character.If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.
代码:-------------------------------------------------------------
/**
* Write the String data
*
* @param out
* @param value
*/
public static void writeUnicode(final DataOutputStream out, final String value) throws ActionException {
try {
final String unicode = StringFormatter.gbEncoding( value );
final byte[] data = unicode.getBytes();
final int dataLength = data.length;
System.out.println( "Data Length is: " + dataLength );
System.out.println( "Data is: " + value );
out.writeInt( dataLength ); //先写出字符串的长度
out.write( data, 0, dataLength ); //然后写出转化后的字符串
} catch (IOException e) {
throw new ActionException( IMDefaultAction.class.getName(), e.getMessage() );
}
}
----------------------------------------------------------------------
以下代码是gbEncoding()方法,把双字节字符转换成\uXXXX,ASIIC码在前面补00。
----------------------------------------------------------------------
/**
* This method will encode the String to unicode.
*
* @param gbString
* @return
*/
Thank you for you detailed help on Chinese. However, I am still a little confused. I have problems to let manifest/jad file show Chinese.
I tried to put unicode, escaped unicode, native Chinese written from a Chinese OS into manifest/jad file, but they did not work. Now, I saw your instructions for put UTF-8 code, I tried, I could not make it work either. I think I must have done something wrong.
I want to show
MIDlet-Name: 空战
I put into manifest/jad:
MIDlet-Name: E7A9BAE68898
When I download the game from my Chinese handset, it still shows the samething: E7A9BAE68898, not 空战
Can you tell me what went wrong?
Thanks a lot!
Can you give me a simple example
2004-01-22, 18:30#8
This is a very good discussion on Chinese development issues.
Another related issue is how to pass Chinese XML data between a MIDP and server side applications. For example, a MIDP gets Chinese data in XML from a servlet and displays the data in UI or a MIDP posts Chinese data to a servlet and the servlet stores the data into a database table. Can anyone share his/her experience here?