Two Laptop Bag
The moose likes I/O and Streams and the fly likes Stream to String and vice versa 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 "Stream to String and vice versa" Watch "Stream to String and vice versa" New topic

Stream to String and vice versa

Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149

i need to do some object-serializing from/to String variables.

I'm not very familiar with this stream stuff so please could someone
post some code particles.

I use the XMLEncoder/XMLDecoder Classes from java.beans

I either have an Object and want its XML representation written to a String.
Or i have the String with the XML representation and want to recreate the original Object.

java.beans gives me these two Classes (See API Doc Quotes) that work like a charm if i read/write from/to files.

So how would i go on with Strings insted. A surplus would be if we could have the Strings zip compressed/decompressed on reading/writing.

(This XML is HUGE)

Only thing i could find was some half-cooked stuff that changed all the beautiful UTF-8 characters in the XML to ??. Dang!


public XMLEncoder(OutputStream out)

Creates a new output stream for sending JavaBeans to the stream out using an XML encoding.

out - The stream to which the XML representation of the objects will be sent.
See Also:


public XMLDecoder(InputStream in)

Creates a new input stream for reading archives created by the XMLEncoder class.

in - The underlying stream.
See Also:

You really could make my day here.

Thank you,

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look into ByteArrayInputStream and ByteArrayOutputStream. Do they do what you need?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
No i don't think so, the byteXX stuff chops my beautiful UTF-8 characters to pieces.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Check out StringReader and StringWriter (which uses a StringBuffer despite the name). These classes will correctly handle character conversion.
I agree. Here's the link:
subject: Stream to String and vice versa
It's not a secret anymore!