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 / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

Print the output on the screen from a class that uses PrintWriter as an output stream.

Thread view: 
saturnlee@yahoo.com - 03 Dec 2006 22:24 GMT
I 'm trying to output the result from a class called lexical, and CLASS
lexical uses  PrintWriter as an output stream.

I successfully output the result to the file using the following
command

        lexical.outStream=new PrintWriter( new
FileOutputStream("testing.txt") );

but, how can i output the result, and print the result on the screen? I
try the following
               OutputStream out=new OutputStream();
        lexical.outStream = new PrintWriter( new PrintStream(out));
but it doesn't work.

Any suggestion?
Thomas Fritsch - 03 Dec 2006 23:56 GMT
>I 'm trying to output the result from a class called lexical, and CLASS
> lexical uses  PrintWriter as an output stream.
[quoted text clipped - 12 lines]
>
> Any suggestion?
lexical.outStream = new PrintWriter(System.out);

System.out is a predefined PrintStream printing on the console. It is
documented at
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html

Signature

Thomas

warewolf - 04 Dec 2006 02:13 GMT
> >I 'm trying to output the result from a class called lexical, and CLASS
> > lexical uses  PrintWriter as an output stream.
[quoted text clipped - 20 lines]
> --
> Thomas

First off, you can't actually create an new OutputStream, because
OutputStream is an interface rather than a class.  Second, I doubt that
Thomas's suggestion will do what it is that you want it to do, since it
will reassign the output from the stream to the console, and not write
to the file.

To print to the console as well, you will either have to use the
System.out.print() method every time you write to the file (with the
same parameters) (Thomas's link will explain the System.out.print()
method, and its cousin, the System.out.println() method), or edit the
lexicon class so that it will do this for you every time you write
something to the file.

Depending on your experience in Java, you could also subclass
FileOutputStream, and overload its write() method to use
System.out.print() in addition to whatever it normally does.  If you do
that, then remember to set lexicon's outputStream in a way to reflect
this change.

-0daedalus
Dale King - 08 Dec 2006 13:38 GMT
> First off, you can't actually create an new OutputStream, because
> OutputStream is an interface rather than a class.

For the record, OutputStream is an abstract class, not an interface.

Signature

 Dale King

Patricia Shanahan - 04 Dec 2006 04:36 GMT
> I 'm trying to output the result from a class called lexical, and CLASS
> lexical uses  PrintWriter as an output stream.
[quoted text clipped - 12 lines]
>
> Any suggestion?

Here's an idea - write a Writer subclass that feeds the output to
multiple Writer instances. I've done very little testing. Use at your
own risk:

import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * Write to multiple Writer instances
 *
 */

public class MultiWriter extends Writer {

  private Writer[] writers;

  /**
   * New MultiWriter for specified writers. Note that flush is
   * only done on explicit call.
   */
  public MultiWriter(Writer... writers) {
    this.writers = new Writer[writers.length];
    for (int i = 0; i < writers.length; i++) {
      this.writers[i] = writers[i];
    }
  }

  @Override
  public void write(char[] cbuf, int off, int len)
      throws IOException {
    for (Writer w : writers) {
      w.write(cbuf, off, len);
      w.flush();
    }
  }

  @Override
  public void flush() throws IOException {
    for (Writer w : writers) {
      w.flush();
    }
  }

  @Override
  public void close() throws IOException {
    for (Writer w : writers) {
      w.close();
    }
  }

  public static void main(String[] args) throws IOException {
    Writer console = new OutputStreamWriter(System.out);
    Writer file = new FileWriter("Testfile.txt");

    Writer dup = new MultiWriter(console, file);
    PrintWriter print = new PrintWriter(dup);

    print.printf("Hello, world");
    print.println();
  }
}


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.