AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

OutputStream,Printwriter etc... help

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
rivera
Guest





PostPosted: Fri Oct 21, 2005 6:33 pm    Post subject: OutputStream,Printwriter etc... help Reply with quote



hello everyone,

I am having trouble understanding the concept of PrintWriters and
OutputStreams for a program i'm writing.
What i have is a function that takes a printwriter as an argument and writes
to it. I can get it to work on System.out like this

PrintWriter out = new PrintWriter(System.out,true);

function(x,out);

this works perfectly but i dont need to System.out the result i need to
write it to a JTextArea, Can someone explain how i can possibly append the
printwriter to the TextArea object or save it to a string?


Back to top
Oliver Wong
Guest





PostPosted: Fri Oct 21, 2005 6:41 pm    Post subject: Re: OutputStream,Printwriter etc... help Reply with quote




"rivera" <boosh (AT) pacbell (DOT) net> wrote

Quote:
hello everyone,

I am having trouble understanding the concept of PrintWriters and
OutputStreams for a program i'm writing.
What i have is a function that takes a printwriter as an argument and
writes to it. I can get it to work on System.out like this

PrintWriter out = new PrintWriter(System.out,true);

function(x,out);

this works perfectly but i dont need to System.out the result i need to
write it to a JTextArea, Can someone explain how i can possibly append the
printwriter to the TextArea object or save it to a string?

How about something like:

<code>
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw,true);
function(x,out);
String s = sw.toString();
</code>

- Oliver



Back to top
Roedy Green
Guest





PostPosted: Sat Oct 22, 2005 1:22 am    Post subject: Re: OutputStream,Printwriter etc... help Reply with quote



On Fri, 21 Oct 2005 18:33:32 GMT, "rivera" <boosh (AT) pacbell (DOT) net> wrote
or quoted :

Quote:

this works perfectly but i dont need to System.out the result i need to
write it to a JTextArea, Can someone explain how i can possibly append the
printwriter to the TextArea object or save it to a string?

I won't, but a program I wrote will. See
http://mindprod.com/applets/fileio.html


Tell it you want you want to output characters to an char buffer in
memory. Then you can convert that to a String and feed it to your
TextArea.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Back to top
Thomas Fritsch
Guest





PostPosted: Sat Oct 22, 2005 6:08 pm    Post subject: Re: OutputStream,Printwriter etc... help Reply with quote

"rivera" <boosh (AT) pacbell (DOT) net> wrote:
Quote:
hello everyone,

I am having trouble understanding the concept of PrintWriters and
OutputStreams for a program i'm writing.
What i have is a function that takes a printwriter as an argument and
writes to it. I can get it to work on System.out like this

PrintWriter out = new PrintWriter(System.out,true);

function(x,out);

this works perfectly but i dont need to System.out the result i need to
write it to a JTextArea, Can someone explain how i can possibly append the
printwriter to the TextArea object or save it to a string?
You may want to develop a subclass of java.io.Writer, which "prints" into a

JTextArea. This is easier than one might think at first:

/////// begin source
public class TextAreaWriter extends Writer {
private JTextArea textArea;

public TextAreaWriter(JTextArea textArea) {
this.textArea = textArea;
}

public void write(char buf[], int off, int len) throws IOException {
if (textArea == null)
throw new IOException("already closed");
textArea.append(new String(buf, off, len));
textArea.setCaretPosition(textArea.getText().length());
}

public void flush() {
}

public void close() {
textArea = null;
}
}
////// end source
I suggest to re-read the details of the API doc of Writer (especially its 3
abstract methods) to understand why and how the above code works.
http://java.sun.com/j2se/1.4.2/docs/api/java/io/Writer.html

You can create a PrintWriter around it by
PrintWriter out = new PrintWriter(new TextAreaWriter(yourTextArea));
and use that like any PrintWriter
out.println("bla bla...");

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.