com.ccg.util
Class PrintWriterString
java.lang.Object
java.io.Writer
java.io.PrintWriter
com.ccg.util.PrintWriterString
- All Implemented Interfaces:
- Closeable, Flushable, Appendable
public class PrintWriterString
- extends PrintWriter
Handy class for creating formatted string output.
The PrintWriter method is useful for
formatting output. However, that class does not allow one to easily
fetch the formatted output as a String
object. This class simplifies the process of creating a PrintWriter object which can be used to format output
strings. Basically, the class is used in the following manner:
import com.ccg.util.PrintWriterString;
class Example {
public void main(String[] args) {
PrintWriterString pws = PrintWriterString.create();
for (int i = 0; i < args.length; i++) {
pws.println("arg["+i+"]="+args[i]);
}
String s = pws.toString();
System.out.println(s);
}
}
If you look at the above example, you probably notice that it
could have printed directly to System.out. However, had we done
that we could not have retrieved the formatted output as a string
("s"). The usefulness of this becomes more apparent when you are
working with objects that require strings (such as GUI text
widgets). Check out the Demo.java and its associated
Demo.html applet for
an example of this.
- Since:
- 1.0
- Version:
- $Revision: 1.1.1.1 $
- Author:
- $Author: pkb $
| Methods inherited from class java.io.PrintWriter |
append, append, append, checkError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write |
create
public static PrintWriterString create()
- Simple way to create a
PrintWriterString object
Convience function to create the PrintWriterString object. It automatically creates and
associates the StringWriter object for you
and is a little easier to use than the constructor.
After you've created your object, you can use the standard
"print()" and "println()" methods on it. When you are done, use
the toString() method to fetch the results as a
string.
- Returns:
- A
PrintWriterString object which you
can start using for output. - Since:
- 1.0
- See Also:
toString()
toString
public String toString()
- Fetch the formatted output.
After printing to your stream, you can use this method to fetch
the
String representation.
- Overrides:
toString in class Object
- Returns:
- Contents of formatted output as a
String. - Since:
- 1.0
- See Also:
create()
Copyright 1998-1998-2006 null. All Rights Reserved.