• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

readUTF not working

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
I found some pretty shells, some sea glass and this lovely tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic