• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

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
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:
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic