This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes DataInput/OutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DataInput/OutputStream" Watch "DataInput/OutputStream" New topic
Author

DataInput/OutputStream

Arthur Krochin
Greenhorn

Joined: Oct 10, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 10, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 10, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome!
 
jQuery in Action, 2nd edition
 
subject: DataInput/OutputStream