Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DataOutputstream writeshort doesnt work  RSS feed

 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to use the dataoutputstream and the writeshort method to write to a file, but everytime I use it, it just right a small square to the file instead of the number.

Heres my code:

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DataOutputStream writes in binary, not text. Your program wrote the 16-bit binary value for 12 to the file, not the characters "1" and "2". You will not be able to look at a file written by it, using an ordinary text editor.

In your particular editor, you see the "little square", because that is how your editor shows a value that doesn't look like text.

You need to read up more on the various ways of writing data to streams and writers.

BTW it's best not to say that a particular API method "X() doesn't work". This suggests that you think you have found a bug in the Sun Java implementation. While not impossible, it is unlikely that you have. Suggesting it can be read as arrogance, though I'm sure you didn't mean it that way. Better to say "I am having trouble using X()"
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,

If you're trying to write numbers to a text file, look at the java.io.PrintWriter class.
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the title, was late at night. When I use the writeChar() method it writes the character symbol as is to the file, and when I use readChar to read it back it also reads back the same characters.

Is there a way I can do that using the datainput/output stream for shorts, and integers. I know it sounds stupid, but I have to use the datastreams to do this. Thanks in advance
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!