Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

readUTF throws always EOF

Thread view: 
Chameleon - 03 Jan 2006 22:55 GMT
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!
hiwa - 04 Jan 2006 01:01 GMT
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.
Chameleon - 04 Jan 2006 11:14 GMT
> Try using BufferedReader and its readLine() method.
> In my impression gotten from past experiences, DataInputStream
[quoted text clipped - 3 lines]
> 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?
Roedy Green - 04 Jan 2006 01:05 GMT
On Wed, 04 Jan 2006 00:55:08 +0200, Chameleon
<cham_gss@hotmail.NOSPAM.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal - 04 Jan 2006 12:03 GMT
> 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
Chameleon - 04 Jan 2006 16:19 GMT
> the code:
> -----------------
[quoted text clipped - 16 lines]
> 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)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.