File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Character and Binary in one file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Character and Binary in one file" Watch "Character and Binary in one file" New topic
Author

Character and Binary in one file

Rod Nichols
Greenhorn

Joined: Apr 19, 2001
Posts: 23
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
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.


"I'm not back." - Bill Harding, Twister
Rod Nichols
Greenhorn

Joined: Apr 19, 2001
Posts: 23
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
 
 
subject: Character and Binary in one file
 
Threads others viewed
Data File Format and reading header information
B&S: problem in the db file processing
PushbackInputStream
Some Questions About IO
NX: db.db file format
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture