×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Chinese character encoding

    Dear all,

    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.



    Cheers,
    Chandler

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    1,980
    If your original program is

    DataInputStream dataInputStream = new DataInputStream(getClass().getResourceAsStream("data.txt"));

    It has a error on path.

    The correct is

    DataInputStream dataInputStream = new DataInputStream(getClass().getResourceAsStream("/data.txt"));

    And the data.txt must be in the res subdirectory of the project directory.

    r60600

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    I apologize for my typo, I did key in

    DataInputStream dataInputStream = new DataInputStream(getClass().getResourceAsStream("/data.txt"));

    instead of

    DataInputStream dataInputStream = new DataInputStream(getClass().getResourceAsStream("data.txt"));

    Afterwards, the readUTF() operation just keeps throwing EOFException.

    Can somebody come to my rescue? Thanks.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    1,980
    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.

    Maybe you must convert it via array manually.

    r60600

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×