cformat
Class PrintfWriter

java.lang.Object
  |
  +--java.io.Writer
        |
        +--java.io.PrintWriter
              |
              +--cformat.PrintfWriter

public class PrintfWriter
extends java.io.PrintWriter

Writer class to output primitive types using C printf style formatting. For each primitive type (float, double, char, int, long, String), there is a printf method which takes (as a first argument) a printf style format string. Because Java does not permit variable numbers of arguments, each printf function accepts only one primitive type, and the formats can correspondingly contain only one conversion sequence.

For example, suppose we wish to print a list of names and numbers according to the printf format "%s, %8.2f". This can be done as follows:

    String[] names = ...
    double[] score = ...
    PrintfWriter w = new PrintfWriter (writer);
    for (int i=0; i<1000; i++)
     { w.printf ("%s, ", names[i]);
       w.printf ("%8.2f", score[i]);
     }
 

Instead of a format string, one can instead use a pre-parsed PrintfFormat object which is created from a format string. This will be more efficient when a lot of output is required:

    double[] bigVector = new double[1000];
    PrintfWriter w = new PrintfWriter (writer);
    PrintfFormat fmt = new PrintfFormat ("%8.2f");
    for (int i=0; i<1000; i++)
     { w.printf (fmt, bigVector[i]);
     }
 

Finally, PrintfFormat can be used to perform the conversion directly, via its tostr method:

    PrintfFormat fmt = new PrintfFormat ("%8.2f");
    for (int i=0; i<1000; i++)
     { System.out.println (fmt.tostr(bigVector[i]));
     }
 

Author:
John E. Lloyd, Fall 2000
See Also:
PrintfFormat, PrintfStream

Constructor Summary
PrintfWriter(java.io.OutputStream out)
          Creates a PrintfWriter, without automatic line flushing, from an existing OutputStream.
PrintfWriter(java.io.OutputStream out, boolean autoFlush)
          Creates a PrintfWriter from an existing OutputStream.
PrintfWriter(java.io.Writer out)
          Creates a PrintfWriter, without automatic line flushing, from an existing Writer.
PrintfWriter(java.io.Writer out, boolean autoFlush)
          Creates a PrintfWriter from an existing Writer.
 
Method Summary
 void printf(PrintfFormat fmt, char x)
          Prints a char in accordance with the supplied PrintfFormat object.
 void printf(PrintfFormat fmt, double x)
          Prints a double in accordance with the supplied PrintfFormat object.
 void printf(PrintfFormat fmt, float x)
          Prints a float in accordance with the supplied PrintfFormat object.
 void printf(PrintfFormat fmt, int x)
          Prints an int in accordance with the supplied PrintfFormat object.
 void printf(PrintfFormat fmt, long x)
          Prints a long in accordance with the supplied PrintfFormat object.
 void printf(PrintfFormat fmt, java.lang.String x)
          Prints a String in accordance with the supplied PrintfFormat object.
 void printf(java.lang.String fs, char x)
          Prints a char in accordance with the supplied format string.
 void printf(java.lang.String fs, double x)
          Prints a double in accordance with the supplied format string.
 void printf(java.lang.String fs, float x)
          Prints a float in accordance with the supplied format string.
 void printf(java.lang.String fs, int x)
          Prints an int in accordance with the supplied format string.
 void printf(java.lang.String fs, long x)
          Prints a long in accordance with the supplied format string.
 void printf(java.lang.String fs, java.lang.String x)
          Prints a String in accordance with the supplied format string.
 
Methods inherited from class java.io.PrintWriter
checkError, close, flush, print, print, print, print, print, print, print, print, print, println, println, println, println, println, println, println, println, println, println, write, write, write, write, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintfWriter

public PrintfWriter(java.io.OutputStream out)
Creates a PrintfWriter, without automatic line flushing, from an existing OutputStream.

Parameters:
out - An output stream

PrintfWriter

public PrintfWriter(java.io.OutputStream out,
                    boolean autoFlush)
Creates a PrintfWriter from an existing OutputStream.

Parameters:
out - An output stream
autoFlush - If true, specifies that output flushing will automatically occur when the println() methods are called.

PrintfWriter

public PrintfWriter(java.io.Writer out)
Creates a PrintfWriter, without automatic line flushing, from an existing Writer.

Parameters:
out - A writer

PrintfWriter

public PrintfWriter(java.io.Writer out,
                    boolean autoFlush)
Creates a PrintfWriter from an existing Writer.

Parameters:
out - A writer
autoFlush - If true, specifies that output flushing will automatically occur when the println() methods are called.
Method Detail

printf

public void printf(java.lang.String fs,
                   double x)
Prints a double in accordance with the supplied format string.

Parameters:
fs - Format string
x - Double to output
Throws:
java.lang.IllegalArgumentException - Malformed format string

printf

public void printf(java.lang.String fs,
                   float x)
Prints a float in accordance with the supplied format string.

Parameters:
fs - Format string
x - Float to output
Throws:
java.lang.IllegalArgumentException - Malformed format string

printf

public void printf(java.lang.String fs,
                   long x)
Prints a long in accordance with the supplied format string.

Parameters:
fs - Format string
x - Long to output
Throws:
java.lang.IllegalArgumentException - Malformed format string

printf

public void printf(java.lang.String fs,
                   int x)
Prints an int in accordance with the supplied format string.

Parameters:
fs - Format string
x - Int to output
Throws:
java.lang.IllegalArgumentException - Malformed format string

printf

public void printf(java.lang.String fs,
                   java.lang.String x)
Prints a String in accordance with the supplied format string.

Parameters:
fs - Format string
x - String to output
Throws:
java.lang.IllegalArgumentException - Malformed format string

printf

public void printf(java.lang.String fs,
                   char x)
Prints a char in accordance with the supplied format string.

Parameters:
fs - Format string
x - Char to output
Throws:
java.lang.IllegalArgumentException - Malformed format string

printf

public void printf(PrintfFormat fmt,
                   double x)
Prints a double in accordance with the supplied PrintfFormat object.

Parameters:
fmt - Formatting object
x - Double to output
See Also:
PrintfFormat

printf

public void printf(PrintfFormat fmt,
                   float x)
Prints a float in accordance with the supplied PrintfFormat object.

Parameters:
fmt - Formatting object
x - Float to output
See Also:
PrintfFormat

printf

public void printf(PrintfFormat fmt,
                   long x)
Prints a long in accordance with the supplied PrintfFormat object.

Parameters:
fmt - Formatting object
x - Long to output
See Also:
PrintfFormat

printf

public void printf(PrintfFormat fmt,
                   int x)
Prints an int in accordance with the supplied PrintfFormat object.

Parameters:
fmt - Formatting object
x - Int to output
See Also:
PrintfFormat

printf

public void printf(PrintfFormat fmt,
                   java.lang.String x)
Prints a String in accordance with the supplied PrintfFormat object.

Parameters:
fmt - Formatting object
x - String to output
See Also:
PrintfFormat

printf

public void printf(PrintfFormat fmt,
                   char x)
Prints a char in accordance with the supplied PrintfFormat object.

Parameters:
fmt - Formatting object
x - Char to output
See Also:
PrintfFormat