|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object
|
+--java.io.Writer
|
+--java.io.FilterWriter
|
+--ucb.io.FormatWriter
Provides for printf-like formatted output of text. A typical use of this class for formatted output:
stream.format("%s's age is %d\n").put(name).put(age);
The format command establishes the format for next two calls of the
put member functions.
For convenience, format and put return
the FormatWriter from which they are called to allow the
composition shown. It would have the same effect to write
stream.format("%s's age is %d\n");
stream.put(name);
stream.put(age);
In the absence of a format, or when all items in the last format
are exhausted, output is printed in a default format, depending on
the type of the input item: %d for integer types
and boolean, %g for
floating-point types, and %s for all other types.
Thus, in the
absence of a format, the package behaves similarly to the iostream
package in the standard C++ library, with the put functions
playing the role of the << operator. The member
functions width, precision, and fill
set formatting parameters for the next item sent to
put; this handles the functionality of both the member
functions with those names in C++ and also of '*'
used as a width or precision in a C format string.
| Fields inherited from class java.io.FilterWriter |
out |
| Fields inherited from class java.io.Writer |
lock |
| Constructor Summary | |
FormatWriter(java.io.OutputStream out)
|
|
FormatWriter(java.io.OutputStream out,
boolean flush)
|
|
FormatWriter(java.io.Writer out)
Create a new FormatWriter that sends output to a given stream. |
|
FormatWriter(java.io.Writer out,
boolean flush)
Create a new FormatWriter that sends output to a given stream, and optionally set to flush on each format and put operation. |
|
| Method Summary | |
boolean |
checkError()
Reports I/O errors other than a FormatException. |
void |
close()
Flush and close output stream. |
int |
count()
Reports the number of characters sent to the stream since the latest call to format (or the construction of this
FormatWriter, if there has been no such call). |
void |
disconnect()
Flush the underlying stream and then disassociate it from THIS without closing that stream. |
FormatWriter |
fill(char c)
Set the fill character for the next call to put. |
protected void |
finalize()
At finish, flush all pending output. |
void |
flush()
Flush all pending output, insuring that it gets written. |
FormatWriter |
format(java.lang.String s)
Set the current format. |
FormatWriter |
precision(int n)
Set the precision for the next call to put. |
FormatWriter |
put(boolean x)
Output a boolean as if it were the integer 1 for true, or 0 for false. |
FormatWriter |
put(char c)
Output C as a single character. |
FormatWriter |
put(char[] s)
Output an array of characters as for a string of the same length. |
FormatWriter |
put(double x)
Output a floating-point value according to the current format directive. |
FormatWriter |
put(int x)
Output an int value according to the current format directive. |
FormatWriter |
put(long x)
Output a long value according to the current format directive. |
FormatWriter |
put(java.lang.Object obj)
Output an arbitrary Object as a string. |
FormatWriter |
put(java.lang.String s)
Output a String according to the current format item. |
void |
reopen(java.io.Writer out,
boolean flush)
Reopen THIS to write to OUT, setting automatic flushing of pending output on each operation iff FLUSH. |
FormatWriter |
width(int n)
Set the field width for the next call to put. |
| Methods inherited from class java.io.FilterWriter |
write, write, write |
| Methods inherited from class java.io.Writer |
write, write |
| Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public FormatWriter(java.io.Writer out)
out - the stream that receives formatted output
public FormatWriter(java.io.Writer out,
boolean flush)
out - the stream that receives formatted outputflush - if true, flush pending output on each operation.public FormatWriter(java.io.OutputStream out)
public FormatWriter(java.io.OutputStream out,
boolean flush)
| Method Detail |
public void reopen(java.io.Writer out,
boolean flush)
public void flush()
flush in class java.io.FilterWriterpublic void close()
close in class java.io.FilterWriterpublic void disconnect()
protected void finalize()
finalize in class java.lang.Objectpublic boolean checkError()
public int count()
format (or the construction of this
FormatWriter, if there has been no such call).
public FormatWriter format(java.lang.String s)
throws java.lang.NullPointerException
The format consists of ordinary characters interspersed with formatting directives, introduced with the '%' character. Characters not included in the formatting directives are output as soon as they are encountered. A '%' character may be included by writing it as '%%'.
Each formatting directive consists of
The possible flag characters are as follows:
The conversion codes are as follows:
0x
or 0X, and o conversions force a 0
as the first digit. Treats arguments of type
Boolean, Character, Integer, or Long as booleans, chars,
ints, or longs.
char[] array, a
byte[] array, or any other kind of Object for
which toString yields a value without throwing an
exception. Prints the character or byte string
represented (or returned by toString) as a byte stream
(throwing away the high-order byte of any char values).
If a precision, p is specified, then at most the first
p characters of the string will be printed.
s - the new format stringjava.lang.NullPointerException - the argument is null.
public FormatWriter put(int x)
throws FormatException
d, i, o, u, x, X. Resets the
formatting parameters to their defaults.x - the value to formatFormatException - if the next format directive is
not a valid integral format directive.
public FormatWriter put(long x)
throws FormatException
d, i, o, u, x, X. Resets the
formatting parameters to their defaults.x - the value to formatFormatException - if the next format directive is
not a valid integral format directive.
public FormatWriter put(double x)
throws FormatException
e, f, g. Resets the formatting parameters
to their defaults.x - the value to formatFormatException - if the next format directive is
not a valid floating-point format directive.
public FormatWriter put(boolean x)
throws FormatException
put(int)
public FormatWriter put(char c)
throws FormatException
FormatException - if the next format directive is not
valid for outputing characters.
public FormatWriter put(java.lang.String s)
throws FormatException
s - the string to outputFormatException - if the next format directive is
not a valid string format directive.
public FormatWriter put(char[] s)
throws java.lang.NullPointerException,
FormatException
s - the array to output.FormatException - if the next format directive is
not a valid string format directive.put(java.lang.String)
public FormatWriter put(java.lang.Object obj)
throws FormatException
put on obj.toString().put(java.lang.String)public FormatWriter width(int n)
put.
Sets the default field width for the next call to one of the
put member functions (an explicit field width in a
format item thus overrides this call). The default field width
is reset to 0 after each call on put.n - the default field widthformat(java.lang.String)public FormatWriter precision(int n)
put.
Sets the default precision for the next call to one of the
put member functions (an explicit precision in a
format item thus overrides this call). The default precision
is reset after each call on put.n - the default precisionformat(java.lang.String)public FormatWriter fill(char c)
put.
Sets the default fill character for the next call to one of the
put member functions (an '0' at the beginning of a
field width in a format directive thus overrides this call).
The default fill character is reset to blank after each call on
put.c - the default fill characterformat(java.lang.String)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||