*
The moose likes Beginning Java and the fly likes readUTF not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "readUTF not working" Watch "readUTF not working" New topic
Author

readUTF not working

abhishek kunal
Ranch Hand

Joined: Jul 27, 2009
Posts: 42
i am reading char data from a text file in utf format.....
here is the code

if i don't use readUTF (i used readLine in order to check by the way), then there is no problem , it goes OK, but when i use readUTF, the it throws EOFException.....why is that??


here is output

java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:180)
at java.io.DataInputStream.readUTF(DataInputStream.java:592)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at mainclient.FileInput.main(FileInput.java:49)
BUILD SUCCESSFUL (total time: 1 second)

please help....

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Class DataInputStream is the wrong thing to use when reading from a text file. As the documentation says, class DataInputStream is meant for reading primitive Java data types from an underlying input stream, and readUTF() uses some kind of modified UTF-8 format (not the standard UTF-8 format).

For reading from text files, you should use a Reader. The various Reader classes that exist in java.io (for example BufferedReader, InputStreamReader, FileReader etc.) take care of converting data from an input stream to text, using a character encoding.

To read a text file in UTF-8 encoding, try this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
jQuery in Action, 2nd edition
 
subject: readUTF not working