×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Russia, Bryansk
    Posts
    90

    Reading from Unicode file

    Hello!
    I have a problrm with subj. I use this routine to do it:

    String readFile(String fName){
    StringBuffer str=new StringBuffer();

    try{
    InputStream is=getClass().getResourceAsStream(fName);
    InputStreamReader reader=new InputStreamReader(is,"UTF8");
    int buf;

    while((buf=reader.read())>-1){
    str.append((char)buf);
    }

    }
    catch (Exception e){System.out.println("Res: "+e);}
    return str.toString();
    }

    I get correct characters in the returned String, but they're all preceded by 0. i.e. for string "npuBET" in the file ("Hello" in Russian) I get the String equal to

    ""+(char)0+'n'+(char)0+'p'+(char)0+'u'+(char)0+'B'+(char)0+'E'+(char)0+'T'
    "\0n\0p\0u\0B\0E\0T"

    What is wrong?

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    How are you generating the input file?

    shmoove

  3. #3
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Russia, Bryansk
    Posts
    90
    In different ways. Word. Notepad (with 3 first bytes cut). My own Java program.
    I'm sure it's corect.

    I've found an example in this forum where 0 values are ignored like

    if (buf!=0)
    str.append((char)buf);
    }

    see http://discussion.forum.nokia.com/fo...nicode+file%2A

    Is it correct?
    Last edited by xdimas; 2004-03-18 at 06:03.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    I have seen people refer to this problem before, apparently as a bug. Your fix should be OK, assuming you don't have any intentional '\0's in your text!

    I use my own UTF8 decoder (http://discussion.forum.nokia.com/fo...threadid=31827), as some devices (I'm told) don't support UTF8 as an encoding specification when opening streams or converting between bytes and Strings.

    Graham.

Posting Permissions

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