(1) After finishing writing the file, read it back into a byte array. Easy, but maybe a bit inefficient.
(2) Use a "tee stream" to send the data to a ByteArrayOutputStream as well as to a FileOutputStream. You'd need an OutputStreamWriter in the chain somewhere. There's no such thing as a "tee stream" in Java 1.4, which I currently use, so I have my own. Maybe later Java, or third-party free library, has one.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Considering the file is referred to as "temp", I'm not sure it's even needed. Peter's solutions assume you want to write a file and get a byte array. If you don't need a file, and just want the byte, then you can write it directly:
Incidentally, in this example the BufferedWriter doesn't really do anything useful; you might as well omit it and write with the OutputStream. [ April 10, 2007: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Oct 30, 2001
Indeed. If you were only creating the file as part of your attempt to get the byte array, then Jim's solution is much better. On the other hand, if you really did need a file containing the bytes as well, then either of my suggestions is still valid.