File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "readUTF not working" Watch "readUTF not working" New topic

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
at mainclient.FileInput.main(
BUILD SUCCESSFUL (total time: 1 second)

please help....

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14915

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 (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 8 API documentation
subject: readUTF not working
jQuery in Action, 3rd edition