 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
rivera Guest
|
Posted: Fri Oct 21, 2005 6:33 pm Post subject: OutputStream,Printwriter etc... help |
|
|
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
|
Posted: Fri Oct 21, 2005 6:41 pm Post subject: Re: OutputStream,Printwriter etc... help |
|
|
"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
|
Posted: Sat Oct 22, 2005 1:22 am Post subject: Re: OutputStream,Printwriter etc... help |
|
|
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
|
Posted: Sat Oct 22, 2005 6:08 pm Post subject: Re: OutputStream,Printwriter etc... help |
|
|
"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 |
|
 |
|
|
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
|
|