I read through zhaofei's posting regarding Chinese encoding on Nokia's Developer Discussion Board, and I must say that he'd done a terrific job! Such an altruistic deed is highly admirable.
Unfortunately, with my limited ken, I've encountered a problem that I don't know how to solve. That is the reason why I'm posting the question here, hoping any of you to give me sound advice.
I've listed down the steps I went through as follows:
1) Using UltraEdit, I created a text file and input some chinese characters.
2) The text file was converted from Unicode to UTF-8 by using the conversion option "Unicode/UTF-8 to UTF-8 (Unicode Encoding)" available in UltraEdit.
3) Then I tried to read the contents of the text file using the following code.
DataInputStream dataInputStream = new DataInputStream(getClass().getResourceAsStream("data.txt"));
String string = dataInputStream.readUTF();
4) Unfortunately, the readUTF() operation keeps throwing EOFException.
Do you know what is wrong here? I'd appreciate if you could enlighten.
Thanks a million.
The EOFException error maybe the length of the DataInputStream created from data.txt is not fit for the UTF-8 encoding way.
It is the explanation of 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.