Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > readUTF throws always EOF

Reply
Thread Tools

readUTF throws always EOF

 
 
Chameleon
Guest
Posts: n/a
 
      01-03-2006
the code:
-----------------
static public String loadFile(String s) throws Exception {
DataInputStream isr = new
DataInputStream(LanguageLoader.class.getResourceAs Stream(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!
 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      01-04-2006
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.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-04-2006
On Wed, 04 Jan 2006 00:55:08 +0200, Chameleon
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Chameleon
Guest
Posts: n/a
 
      01-04-2006
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?
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      01-04-2006
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


 
Reply With Quote
 
Chameleon
Guest
Posts: n/a
 
      01-04-2006
Chameleon wrote:
> the code:
> -----------------
> static public String loadFile(String s) throws Exception {
> DataInputStream isr = new
> DataInputStream(LanguageLoader.class.getResourceAs Stream(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.getResourceAs Stream(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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Jan Burse Java 67 03-14-2012 12:21 AM
Reliability of writeUTF / readUTF korcs Java 7 11-29-2007 05:58 AM
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
What's wrong with my use of DataInputStream.readUTF() ?? rhimbo Java 2 03-24-2006 06:52 PM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM



Advertisments