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 / GUI / October 2005

Tip: Looking for answers? Try searching our database.

write() question

Thread view: 
Huub - 26 Oct 2005 05:37 GMT
Hi,

I notice 2 write(..) methods in the API that can be used with RTFEditorkit:

write(OutputStream, Document, int, int) and

write(Writer, Document, int, int)

Which one could I take best to write to a file and why?

Thanks,

Huub
Roedy Green - 26 Oct 2005 05:57 GMT
>write(OutputStream, Document, int, int) and
>
>write(Writer, Document, int, int)

OutputStreams are for writing raw bytes. In the olden days they were
also used for writing Strings out in locale default 8-bit encoding.

Writers are for writing Strings out in locale specific 8-bit
encodings. You get to choose which one explicitly.

Today, you choose this way. If your data are raw bytes, use
OutputStream. If your data are char[] or String use a Writer.

I have never experimented with different encodings on the RTF.  I
would be tempted to use the byte encoding myself, though that violates
my rule of thumb since it would most likely use some standard MS
encoding.

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

Often it is quicker just to try both and see which works better.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Huub - 26 Oct 2005 07:07 GMT
> Writers are for writing Strings out in locale specific 8-bit
> encodings. You get to choose which one explicitly.
> Often it is quicker just to try both and see which works better.

I have a strange(?) thing here. According to the API, read() and write()
both want "doc" to be of the type document, not HTMLdocument. Yet, with
kit.read() doc is of the latter type without problem (right?). With
kit.write() though, it doesn't accept doc being of type HTMLdocument.
And as far as I can find out, it can't be casted to Document. A (kind
of) similar problem is with "sr". Any solution to this? (I searched
Mindprod but found nothing.)

        doc = (HTMLDocument)getEditor().getDocument();
        StringReader sr = new StringReader(org+html);
        kit.read(sr, doc, 0);
        sr.close();
        kit.write(sr, doc, 0, sr.toString().length());

Thank you.
BTW: without kit.write(), the code works well.
Roedy Green - 26 Oct 2005 07:19 GMT
>I have a strange(?) thing here. According to the API, read() and write()
>both want "doc" to be of the type document, not HTMLdocument. Yet, with
>kit.read() doc is of the latter type without problem (right?).

You have to be careful:

there is

javax.swing.text.Document an interface

org.w3c.dom.Document an interface (not the one you want. That is XML)

Then there is javax.swing.text.html.HTMLDocument a class that
implements  Document.

When a method calls for an interface Document, there is no such
animal. You cannot say Document d= new Document(....);

That is just shorthand for, "given me a reference to some object that
IMPLEMENTS Document", i.e. an HTMLDocument.

There is thus no need to cast HTMLDocument to a Document.

I suspect you may still be fuzzy on the differences between classes
and interfaces and abstract classes.

see http://mindprod.com/jgloss/interface.html
http://mindprod.com/jgloss/interfacevsabstract.html
http://mindprod.com/jgloss/abstractclass.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Huub - 26 Oct 2005 07:54 GMT
> There is thus no need to cast HTMLDocument to a Document.
>
[quoted text clipped - 4 lines]
> http://mindprod.com/jgloss/interfacevsabstract.html
> http://mindprod.com/jgloss/abstractclass.html

Thank you for this info. If there is thus no need for casting, why does
it give the error:

The method write(Writer, Document, int, int) in the type HTMLEditorKit
is not applicable for the arguments (StringReader, HTMLDocument, int, int)

Thank you.
Roedy Green - 26 Oct 2005 08:08 GMT
>The method write(Writer, Document, int, int) in the type HTMLEditorKit
>is not applicable for the arguments (StringReader, HTMLDocument, int, int)

you are barking up the wrong tree.

Questions:

Is a StringReader a flavour of Writer?

Is an HTMLDocument at type of Document?

is an int a type of int?

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Huub - 26 Oct 2005 08:27 GMT
>>The method write(Writer, Document, int, int) in the type HTMLEditorKit
>>is not applicable for the arguments (StringReader, HTMLDocument, int, int)
>
> you are barking up the wrong tree.

Ummm...I take it that you mean I'm missing the point you're making.

> Questions:
>
> Is a StringReader a flavour of Writer?

No, it's not. So I have to change it into something that is.

> Is an HTMLDocument at type of Document?

As I understood from your post, it isn't but you can use it.

> is an int a type of int?

int = int.

Thank you.
Roedy Green - 26 Oct 2005 11:07 GMT
>> you are barking up the wrong tree.
>
>Ummm...I take it that you mean I'm missing the point you're making.

Oops.  Hmm. English idioms don't make any sense literally translated.

Imagine you were a dog, and you desperately wanted to bite a cat.
The cat is sitting in tree A,  but the dog in jumping up and barking
to get at the non-existent cat in tree B which he is convinced in the
only possible place a cat could hide.

Tree A = first parameter

Tree B = second parameter.

cat = source of your bug.

Your are going to burst out laughing when you see it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Huub - 26 Oct 2005 11:35 GMT
>>>you are barking up the wrong tree.
>>
>>Ummm...I take it that you mean I'm missing the point you're making.
>
> Oops.  Hmm. English idioms don't make any sense literally translated.

It may come as a surprise, but English is not my first language.
Although many people told me I'm fluent in it, I know I still miss things.

> Imagine you were a dog, and you desperately wanted to bite a cat.
> The cat is sitting in tree A,  but the dog in jumping up and barking
[quoted text clipped - 8 lines]
>
> Your are going to burst out laughing when you see it.

I'll take your word for it.
Roedy Green - 26 Oct 2005 12:35 GMT
>It may come as a surprise, but English is not my first language.

When I visited the Netherlands I met only one person who did not speak
English.  That was quite a surprise for me.

Even the one who did not, knew enough English to explain it would be
hopeless to try to communicate.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Huub - 26 Oct 2005 09:36 GMT
I changed it like this:

    OutputStream os;
    kit.write(os, doc, 0, sr.toString().length());

This gives NullPointerException. I guess because "os" should be
initialized. But as what? Bytes[] or a filename? Or, e.g. like the API
states: os.toString()?

Thank you.
Roedy Green - 26 Oct 2005 11:10 GMT
>    OutputStream os;
>    kit.write(os, doc, 0, sr.toString().length());
>
>This gives NullPointerException. I guess because "os" should be
>initialized. But as what? Bytes[] or a filename? Or, e.g. like the API
>states: os.toString()?

At wants an OutputStream object. You gave it a null. No wonder it is
annoyed with you.There are many flavours OutputStreams.

See http://mindprod.com/applets/fileio.html

Will acquaint you with them. Just tell the amanuensis you want to
write raw bytes to something and look at all your possible choices.
Hint: you most likely want an ordinary file.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Huub - 26 Oct 2005 11:49 GMT
> See http://mindprod.com/applets/fileio.html
>  
> Will acquaint you with them. Just tell the amanuensis you want to
> write raw bytes to something and look at all your possible choices.
> Hint: you most likely want an ordinary file.

Yes, needed FileOutputStream and passing on the filename. It's working
now. Thank you very much for your (teethgrinding?) patience.
Roedy Green - 26 Oct 2005 12:35 GMT
>Yes, needed FileOutputStream and passing on the filename. It's working
>now. Thank you very much for your (teethgrinding?) patience.

A am hoping that I will get you hooked and all this effort coaching
you will not be tossed aside.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 26 Oct 2005 12:46 GMT
> I notice 2 write(..) methods in the API that can be used with RTFEditorkit:
>
[quoted text clipped - 3 lines]
>
> Which one could I take best to write to a file and why?

Doesn't one of them always throw an IOException?

Although RTF looks like a text format and one of its MIME types starts
with text/, it isn't.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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.