readUTF throws always EOF

C

Chameleon

the code:
-----------------
static public String loadFile(String s) throws Exception {
DataInputStream isr = new
DataInputStream(LanguageLoader.class.getResourceAsStream(s));
return isr.readUTF();
}
 
H

hiwa

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.
 
R

Roedy Green

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.
 
C

Chameleon

hiwa said:
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?
 
C

Chris Uppal

Chameleon said:
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
 
C

Chameleon

Chameleon said:
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:

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top