Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Newbie output to byte array question

Thread view: 
fiziwig - 16 Aug 2006 19:06 GMT
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...
Bill Medland - 16 Aug 2006 19:21 GMT
> 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...
[quoted text clipped - 7 lines]
>
>         FileWriter writer = null;

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

>         try {
>           writer = new FileWriter("test.txt");
[quoted text clipped - 5 lines]
>         catch (IOException ex) {
>             ... etc...

Signature

Bill Medland

fiziwig - 16 Aug 2006 19:44 GMT
> > 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)

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
dsjoblom@abo.fi - 16 Aug 2006 21:13 GMT
> > > 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.
[quoted text clipped - 4 lines]
> 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
Ian Shef - 16 Aug 2006 21:20 GMT
dsjoblom@abo.fi wrote in news:1155759214.511611.62740
@i42g2000cwa.googlegroups.com:

>> > > 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.
[quoted text clipped - 9 lines]
> Regards,
> Daniel Sjöblom

You  may want to try Roedy Green's File I/O Amanuensis at
http://www.mindprod.com/applets/fileio.html

Signature

Ian Shef     805/F6      *    These are my personal opinions    
Raytheon Company         *    and not those of my employer.
PO Box 11337             *
Tucson, AZ 85734-1337    *

Matt Humphrey - 16 Aug 2006 20:13 GMT
> 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.
[quoted text clipped - 18 lines]
>        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 matth@ivizNOSPAM.com http://www.iviz.com/
fiziwig - 16 Aug 2006 22:13 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.