wood burning stoves*
The moose likes I/O and Streams and the fly likes DataOutputStream problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "DataOutputStream problem" Watch "DataOutputStream problem" New topic
Author

DataOutputStream problem

Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
I am just hitting a wall... must me something simple but I am still trying to find out why the code below print garbage in the file

Thanks
[ May 03, 2004: Message edited by: Leslie Chaim ]

Normal is in the eye of the beholder
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

Garbage you say? Then why does this code:

print out the following:

The same data you wrote to the file!
If you want to write a text file rather than a binary one (i.e. be able to read your file without a hex editor), use the Writer classes. Look at the Java Tutorial: IO for more info.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
OK, got it...
Now I will try and figure out how to link DataOutputStream to a FileWriter so I can do a writeInt().
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Needless to say that this works Thanks Joe!
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Question on efficiency here: what if I only print strings instead of
dos.writeInt (seq);
I'll say:
dos.writeBytes (seq + "");
Effectively, my file would be a text file in the end (or good enough for what I need).
Which is more efficient the Writer or Stream classes?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well using dos.writeInt() will be more efficient, I'm pretty sure, but it's one of those situations where it's very unlikely you'll ever notice a difference unless you're writing a lot of data. Generally, the most important factor is how will you parse it? Using readInt() is pretty simple, if the data was written with writeInt(). Parsing text data can be more complexx, as you have to know more about the delimiters used in the file. But using a text format has one big advantage - humans can read and understand your file, just by looking at it. With bainary formats, they can't. So if you want humans to be able to do this, that often outweighs any other considerations.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

Note that you just wrote 456458. When the file is read, how will you no its not (4, 56458) or (4564, 58)? You probably need to insert some sort of delimiter.
 
jQuery in Action, 2nd edition
 
subject: DataOutputStream problem
 
Similar Threads
IO doubt
input/output
write problem in text file
stream output to reader input?
I/O BufferedInputStream