• Post Reply Bookmark Topic Watch Topic
  • New Topic

Character and Binary in one file

 
Rod Nichols
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am wondering if it is possible to write character and binary data to the same file. I am working on a file that is to be uploaded to our mainframe and if I could put the numeric fields in binary along with the character date in the same file on the pc, it would save some processing on the mainframe.
Here are some other thoughts about this :
It appears that each stream either does 16 bit character or 8 bit binary processing. Can two steams point to the same output file at the same time ? That way I could write some character data from the character stream, then write the numeric, binary fields from the binary stream.
Also, what if I built my character data as strings and just wrote them out as binary along with the binary data, would that work ?
Thank You in advance !!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yesy you can, but I'd be very careful of how contents are written to the file.
Here is some code showing that it is possible to do what you want (I guess):

The output is:
Wrote 65 66 67 68 69
Wrote Hello
Read 65 66 67 68 69
Read Hello
And the file mixed.dat looks like:
ABCDEHello
The code is not optimized at all, you should perform some checks, modularize it a little bit and catch some exceptions...
Let me know if you need more...
[ February 21, 2002: Message edited by: Valentin Crettaz ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest that to do something like this, you would be best off using DataOutputStream and DataInputStream for everything. The have writeUTF() and readUTF() methods which handle 16-bit characters fine (while using only 8 bits for most common characters). If you try to read back strings written by Writers, the question is - how do you know when the string ends? You can very easily end up sucking up additional binary data into your string. There are ways around this - put all strings at the end, always end strings with newlines, etc. But the DataOutputStream method is far more robust - it will write the length of the entire string at the beginning. So when you read with a DataInputStream, it will know exactly how big the string is, and when to stop. Additionally, using DOS/DIS you don't have to worry about the additional headaches of switching from reader/writer to stream and vice versa, making sure the streams are flushed before you switch, etc. Lots of things can go wrong here if you're not careful, especially on the read.
 
Rod Nichols
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin and Jim,
Thanks to both of you, that is exactly what I was looking for. I thought you could do it but I wasn't sure. This would be for a file to be uploaded to out mainframe so the mix would be okay. If this was going to be read into another Java program I would do something different.
Rod
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!