Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

readUTF not working  RSS feed

 
abhishek kunal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 15860
80
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!