readUTF throws always EOF

Discussion in 'Java' started by Chameleon, Jan 3, 2006.

  1. Chameleon

    Chameleon Guest

    the code:
    -----------------
    static public String loadFile(String s) throws Exception {
    DataInputStream isr = new
    DataInputStream(LanguageLoader.class.getResourceAsStream(s));
    return isr.readUTF();
    }
    -----------------
    I try with ansi file, with UC2 file and UTF-8 file.
    Always throws EOF.

    Every time, the contents of file are:
    -----------------
    english=english
    german=german
    italiano=italiano
    -----------------

    what is the problem?
    thanks!
     
    Chameleon, Jan 3, 2006
    #1
    1. Advertising

  2. Chameleon

    hiwa Guest

    Try using BufferedReader and its readLine() method.
    In my impression gotten from past experiences, DataInputStream
    and its readUTF() method are semi-deprecated APIs.
    They are not simpile nor easy to use and can often bring troubles.
    Read the API documentation for DataInput#readUTF() method.
    Be amazed at its complicatedness and awkwardness of the
    description.
     
    hiwa, Jan 4, 2006
    #2
    1. Advertising

  3. Chameleon

    Roedy Green Guest

    On Wed, 04 Jan 2006 00:55:08 +0200, Chameleon
    <> wrote, quoted or indirectly quoted
    someone who said :

    >I try with ansi file, with UC2 file and UTF-8 file.
    >Always throws EOF.


    Someone just made this same mistake in a another post.

    readUTF is NOT for reading UTF-16 or UTF-8 text files. It is for
    reading binary format files containing counted strings with binary
    lead counts.

    you want a FileReader using explicit UTF-8 encoding.

    see http://mindprod.com/applets/fileio.html
    for the sample code.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 4, 2006
    #3
  4. Chameleon

    Chameleon Guest

    hiwa wrote:
    > Try using BufferedReader and its readLine() method.
    > In my impression gotten from past experiences, DataInputStream
    > and its readUTF() method are semi-deprecated APIs.
    > They are not simpile nor easy to use and can often bring troubles.
    > Read the API documentation for DataInput#readUTF() method.
    > Be amazed at its complicatedness and awkwardness of the
    > description.
    >


    yes but I forgot to say that my app runs on mobiles and the api is simpler.

    Maybe if I try loop with getChar?
     
    Chameleon, Jan 4, 2006
    #4
  5. Chameleon

    Chris Uppal Guest

    Chameleon wrote:

    > I try with ansi file, with UC2 file and UTF-8 file.
    > Always throws EOF.


    Despite its name, DataInputStream.readUTF() does NOT read UTF-8 or any other
    Unicode format.

    If you want to read Unicode text, use a Reader with an appropriate character
    encoding.

    -- chris
     
    Chris Uppal, Jan 4, 2006
    #5
  6. Chameleon

    Chameleon Guest

    Chameleon wrote:
    > the code:
    > -----------------
    > static public String loadFile(String s) throws Exception {
    > DataInputStream isr = new
    > DataInputStream(LanguageLoader.class.getResourceAsStream(s));
    > return isr.readUTF();
    > }
    > -----------------
    > I try with ansi file, with UC2 file and UTF-8 file.
    > Always throws EOF.
    >
    > Every time, the contents of file are:
    > -----------------
    > english=english
    > german=german
    > italiano=italiano
    > -----------------
    >
    > what is the problem?
    > thanks!


    with this code I can read UC-16 text files BUT with big endian char
    storing (I created a php script for precompile convertion from little to
    big endian)
    ------------------------------
    static public String loadFile(String s) throws Exception {
    DataInputStream isr = new
    DataInputStream(LanguageLoader.class.getResourceAsStream(s));
    s = "";
    int z = isr.available() / 2;
    for(; z > 0; z--)
    s += isr.readChar();
    return s;
    }
    ------------------------------
    the problem is that I run readChar() inside a loop and I don't trust
    speed of such operations in Java (specially java for mobiles)
     
    Chameleon, Jan 4, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rhimbo
    Replies:
    2
    Views:
    5,612
    Roedy Green
    Mar 24, 2006
  2. Kobu
    Replies:
    10
    Views:
    657
    Keith Thompson
    Mar 4, 2005
  3. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    713
    James Kanze
    Jun 15, 2007
  4. korcs
    Replies:
    7
    Views:
    1,026
    Esmond Pitt
    Nov 29, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,109
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page