Newbie output to byte array question

F

fiziwig

I've Googled this, and looked around in the tutorials, but I haven't
found a simple example of how to assign an output to a byte array.
Maybe I'm just too much of a newbie to know how to navigate the
documentation, but...

What I need is for this code (which does exactly what I want) to put
the output into a byte array instead of into the file "test.txt". How
do I accomplish that?

Thanks,
--gary

FileWriter writer = null;
try {
writer = new FileWriter("test.txt");
MinimalHTMLWriter htmlWriter = new
MinimalHTMLWriter(writer,
(StyledDocument)textRegion.getDocument());
htmlWriter.write();
}
catch (IOException ex) {
... etc...
 
B

Bill Medland

fiziwig said:
I've Googled this, and looked around in the tutorials, but I haven't
found a simple example of how to assign an output to a byte array.

Are you sure you really mean a byte array, or will a char array do? (and
then you could encode it I suppose)
Maybe I'm just too much of a newbie to know how to navigate the
documentation, but...

What I need is for this code (which does exactly what I want) to put
the output into a byte array instead of into the file "test.txt". How
do I accomplish that?

Thanks,
--gary

FileWriter writer = null;

I'm new to all this but couldn't you use a CharArrayWriter?
 
F

fiziwig

Bill said:
Are you sure you really mean a byte array, or will a char array do? (and
then you could encode it I suppose)

I'm not sure. I just need to be able to treat the HTML created by
"write()" as a String.
I'm new to all this but couldn't you use a CharArrayWriter?

I don't see why not. I'll have to do some more digging I guess.

Thanks,

--gary
 
M

Matt Humphrey

fiziwig said:
I've Googled this, and looked around in the tutorials, but I haven't
found a simple example of how to assign an output to a byte array.
Maybe I'm just too much of a newbie to know how to navigate the
documentation, but...

What I need is for this code (which does exactly what I want) to put
the output into a byte array instead of into the file "test.txt". How
do I accomplish that?

Thanks,
--gary

FileWriter writer = null;
try {
writer = new FileWriter("test.txt");
MinimalHTMLWriter htmlWriter = new
MinimalHTMLWriter(writer,
(StyledDocument)textRegion.getDocument());
htmlWriter.write();
}
catch (IOException ex) {
... etc...

ByteArrayOutputStream bos = new ByteArrayOutputStream ();
OutputStreamWriter writer = new OutputStreamWriter (bos, encoding);

// do your writing as above

byte [] result = bos.toByteArray ();

The key here is that you must know the encoding you want to use to convert
the characters in the String into bytes. Check the API for
OutputStreamWriter to see some alternatives for specifying the encoding and
then find out what encoding you want. If you don't specify it will use the
default one of your platform, which means those byte arrays will only be
interpretable by machines that use the same encoding.

http://mindprod.com/jgloss/encoding.html

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
D

dsjoblom

fiziwig said:
I'm not sure. I just need to be able to treat the HTML created by
"write()" as a String.

In that case, perhaps StringWriter will do the trick ;-)

Regards,
Daniel Sjöblom
 
F

fiziwig

Thanks for all the suggestions. I went with this solution, which works
fine:

CharArrayWriter writer = null;
try {
writer = new CharArrayWriter();
MinimalHTMLWriter htmlWriter = new
MinimalHTMLWriter(writer, (StyledDocument)textRegion.getDocument());
htmlWriter.write();
}
catch (IOException ex) {
.... etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top