This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataOutputStream problem

 
Leslie Chaim
Ranch Hand
Posts: 336
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Needless to say that this works Thanks Joe!
 
Leslie Chaim
Ranch Hand
Posts: 336
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic