• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

DataInput/OutputStream

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic