|
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||||
java.lang.Object
|
+--java.io.Writer
|
+--java.io.BufferedWriter
|
+--com.braju.beta.format.FormatWriter
Provides formatted output a'la true C-style printf() with any number of parameters.
This is the format of one conversion specification:
__%__ _________ __ _________ __ __________________ __type__ | | | | | | |__flags__| |__width__| |__./:__precision__|or
% flag* [width] [{.|:}precision] type
Field descriptionflags Justification of output and printing of signs, blanks, decimal points, octal, and hexadecimal prefixes.
width Minimum number of characters (bytes) output.
precision Maximum number of characters (bytes) printed for all or part of the output field, or minimum number of digits printed for integer values. For floating-point values/outputs, the precision will, if prefixed by '.' (period) control the number of digits outputted after the period. If instead the precision is prefixed by ':' (colon) the precision value will control the number of significant digit outputted [extension to ANSI-C]. For more information see examples below.
| Flag | Brief description |
| ^ | Central adjusted, otherwise right adjusted. [not an ANSI-C standard]. |
| - (minus sign) | Left adjusted, otherwise right adjusted. |
| + (plus sign) | Prefix the output value with a sign (+ or -) if the output value is of a signed type. Default is that sign appears only for negative values (-). |
| ' ' (blank) | A nonnegative value will have a space prepended. The + flag overrides the blank flag if both appear, and a positive value will be output with a sign. |
| # (pound) | Alternative form. When used with the o, x, X, b or B formats, the # flag prefixes any output value with 0, 0x, 0X, 0b or 0B, respectively. With the p and P formats the output value will be prefixed with 0x and 0X respectively. When used with the f, e, or E formats, the # flag forces the output value to contain a decimal point in all cases. When used with the c format, the # flag forces the output value to be in UTF-encoding, e.g. \u0041. |
| 0 (zero) | When used with the b, B, d, i, o, x, X, e, E, f, g, or G formats, the 0 flag causes leading 0's to pad the output to the field width. The 0 flag is ignored if the - flag is specified. |
| Width | Brief description |
| * (asterix) | The value for the width will be obtained from the parameter list. If the value is negative the output will be left-justified. |
| n (an integer) | A nonnegative integer for the width; e.g. 8. |
| Precision | Brief description |
| * (asterix) | The value for the precision will be obtained from the parameter list. |
| n (an integer) | A nonnegative integer for the precision; e.g. 3. |
| Conversion type |
Corresponding argument will be printed as |
| b | a binary integer. [extension to ANSI-C]. |
| c | a character. |
| d, i | a decimal integer. |
| e,E | a scientific floating point number; e.g. 1.23E-03. |
| f | a floating point number; e.g. 0.00123. |
| g,G | %e or %E is used if the exponent is less than -4 or greater than or equal to the precision; otherwise %f is used. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. Precision specifies the maximum number of significant digits printed. |
| l,L | a logical value, i.e. a boolean; e.g. true, FALSE. Also support for C-style booleans, i.e. 0 or 1 (<>0). [extension to ANSI-C]. |
| o | an octal integer. |
| p,P | hashValue of an object outputted as a hexadecimal integer. [extension to ANSI-C]. |
| s | a string. All characters in String is printed or as many as precision specifies. |
| x,X | a hexadecimal integer. |
| % | There is no corresponding argument, i.e. To output "%" one have to put "%%" in the format string. |
Example:
FormatWriter out = new FormatWriter(new FileWriter("foo.bar"));
out.write("Please, try to support %3.2f%% %s!\n",
new Parameters(100).add("Java"));
writes following to file foo.bar
Please, try to support 100.00% Java!You can also do like this
Parameters p = new Parameters();
Writer out0 = new OutputStreamWriter(System.out);
FormatWriter out = new FormatWriter(out0);
out.write("Please, try to support %.2f%% %s or, "+
"at least to %:3f%%.\n", p.add(100).add("Java").add(99.9453));
out.write("I heard of companies only supporting it to %:5f.",
p.add(1e-3));
which gives
Please, try to support 100.00% Java or, at least to 99.9%. I heard of companies only supporting it to 0.0010%.
Note that the above examples can be simplified by using the printf & scanf methods defined in com.braju.format.Format.
Note that if your program are supposed to run under Java1.0.2 you should use com.braju.format.FormatOutputStream instead. It works the same.
Format,
Format102,
FormatOutputStream,
Parameters| Constructor Summary | |
FormatWriter(java.io.Writer out)
Create a new character-stream writer whose destination is the given stream. |
|
| Method Summary | |
void |
addConversionParser(com.braju.beta.format.ConversionParser parser)
Add a new parser for the format string, e.g. |
void |
close()
Close the stream, flushing it first [...]. |
FormatString |
compileFormatString(java.lang.String fmt)
Compile a format string and return the result as a FormatString [...]. |
static FormatWriter |
conform(java.io.Writer out)
Static method that always returns an instance of this class regardless of the input is an instance of this class or not [...]. |
void |
flush()
Flush the stream [...]. |
com.braju.beta.format.ConversionParser[] |
getConversionParsers()
Gets the conversion parsers. |
boolean |
getModifyLineSeparators()
Checks wether the automatic replacement of newlines is turned on or off. |
void |
removeConversionParser(com.braju.beta.format.ConversionParser parser)
Remove a previously added format string parser [...]. |
void |
resetFormatStringCompiler()
Resets the internal FormatStringCompiler to the default printf-format string compiler. |
void |
setModifyLineSeparators(boolean activated)
Turns on or off the automatic replacement of all occurenses of newlines ('\r', '\n' and '\r\n') found on the input stream [...]. |
void |
write(char[] cbuf,
int off,
int len)
Write a portion of an array of characters [...]. |
int |
write(FormatString fmtstr,
Parameters parameters)
Write characters in a C-sprintf manner according to the specified FormatString and its related parameters. |
void |
write(int ch)
Write a single character [...]. |
void |
write(java.lang.String str)
Write characters in a C-sprintf manner according to the specified format [...]. |
int |
write(java.lang.String fmt,
Parameters parameters)
Write characters in a C-sprintf manner according to the specified format and its related parameters. |
| Methods inherited from class java.io.BufferedWriter |
newLine, write |
| Methods inherited from class java.io.Writer |
write |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public FormatWriter(java.io.Writer out)
| Method Detail |
public void setModifyLineSeparators(boolean activated)
public boolean getModifyLineSeparators()
public static FormatWriter conform(java.io.Writer out)
out - Stream to be conformed into a format writerpublic void addConversionParser(com.braju.beta.format.ConversionParser parser)
parser - Conversion parser to be addedpublic void removeConversionParser(com.braju.beta.format.ConversionParser parser)
parser - Conversion parser to be removedpublic void resetFormatStringCompiler()
public com.braju.beta.format.ConversionParser[] getConversionParsers()
public FormatString compileFormatString(java.lang.String fmt)
fmt - String containing conversion flags to be compiled
public int write(FormatString fmtstr,
Parameters parameters)
throws ParseException,
java.io.IOException
fmtstr - Format string specifying the outputparameters - Parameters containing values to replace the format flagsjava.io.IOException - If an I/O error occurs
public int write(java.lang.String fmt,
Parameters parameters)
throws ParseException,
java.io.IOException
fmt - Format string specifying the outputparameters - Parameters containing values to replace the format flagsjava.io.IOException - If an I/O error occurs
public void write(java.lang.String str)
throws ParseException,
java.io.IOException
write in class java.io.Writerstr - String to be writtenjava.io.IOException - If an I/O error occurs
public void write(int ch)
throws java.io.IOException
write in class java.io.BufferedWriterjava.io.IOException - If an I/O error occurs
public void write(char[] cbuf,
int off,
int len)
throws java.io.IOException
write in class java.io.BufferedWritercbuf - Array of charactersoff - Offset from which to start writing characterslen - Number of characters to writejava.io.IOException - If an I/O error occurs
public void flush()
throws java.io.IOException
flush in class java.io.BufferedWriterjava.io.IOException - If an I/O error occurs
public void close()
throws java.io.IOException
close in class java.io.BufferedWriterjava.io.IOException - If an I/O error occurs
|
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||||