Java Reference
In-Depth Information
Destinations include OutputStream ,aninstance of File (see Section 9.5),
and any class that implements the new Appendable interface.
The program FormatWriteApp shows how we can use Formatter to
send formatted numerical values to the console rather than using the printf()
method:
import java.io.*;
import java.util.Formatter;
/**
* Demonstrate the java.util.Formatter capabilities for
* formatting primitive types.
**/
public class FormatWriteApp
{
public static void main (String arg[]) {
// Send formatted output to the System.out stream.
Formatter formatter =
new Formatter ((OutputStream)System.out);
formatter.format ( " Text output with Formatter.%n " );
formatter.format ( " Primitives converted to strings:%n " );
boolean a - boolean = false;
byte a - byte = 114;
short a - short = 1211;
int an - int = 1234567;
long a - long = 987654321;
float a - float = 983.6f;
double a - double = -4.297e-15;
formatter.format ( " boolean = %9b %n " , - boolean);
formatter.format ( " byte = %9d %n " , - byte);
formatter.format ( " short = %9d %n " , - short);
formatter.format ( " int = %9d %n " , - int);
formatter.format ( " long = %9d %n " , - long);
formatter.format ( " float = %9.3f %n " ,a - float);
formatter.format ( " double = %9.2e %n " ,a - double);
// Need to flush the data out of the buffer.
formatter.flush ();
formatter.close ();
} // main
} // class FormatWriteApp
 
Search WWH ::




Custom Search