Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataInput/OutputStream

 
Arthur Krochin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! In one of my Java books there is a code that demonstrates the use of Data input and output streams. It looks like this:




The console output is:
Writing i: 10
Writing d: 1023.56
Writing b: true

Reading int: 1214606444
Reading double: 1.1558691291519286E228
Reading boolean: true

They should be the same, yet they're not. Not sure what's going on here. Anybody help?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of the true in

If the file already has stuff in it from some previous text, you'll be appending to the end of it. Then, when you go to read, you'll be reading the junk at the beginning instead of what you just wrote.

[EDIT]
Also, look very closely at these two lines:


See a problem? Here's an example of the DRY (Don't Repeat Yourself) principle, and of why we should use constants (final variables) for magic values.
 
Arthur Krochin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Get rid of the true in

If the file already has stuff in it from some previous text, you'll be appending to the end of it. Then, when you go to read, you'll be reading the junk at the beginning instead of what you just wrote.


Thanks, but it's still not working for some reason. The output remains the same. I delete that TEST.txt file each time by the way.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthur Krochin wrote:
Jeff Verdegan wrote:Get rid of the true in

If the file already has stuff in it from some previous text, you'll be appending to the end of it. Then, when you go to read, you'll be reading the junk at the beginning instead of what you just wrote.


Oh wow, the stupidity. Thank you very much! You saved me a lot of time. Now onwards to fix it.



Check the edit to my response. That wasn't the problem. Or at least, not the only problem.
 
Arthur Krochin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Arthur Krochin wrote:
Jeff Verdegan wrote:Get rid of the true in

If the file already has stuff in it from some previous text, you'll be appending to the end of it. Then, when you go to read, you'll be reading the junk at the beginning instead of what you just wrote.


Oh wow, the stupidity. Thank you very much! You saved me a lot of time. Now onwards to fix it.



Check the edit to my response. That wasn't the problem. Or at least, not the only problem.


Oh wow. I need to strangle myself or something. Thanks a lot! Works perfectly now.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic