Home : Course Map : Chapter 9 : Java :
Formatter Output to a File
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The following program, FormatWriteFileApp, follows closely to that of FormatWriteApp discussed in the Formatter to Console section. There we used the java.util.Formatter class to send formatted output to the console. However, FormatWriteFileApp program instead sends output to a file.

The Formattter class does much of the work. We don't even have to create a FileWriter stream as we did in the previous section. We simply pass the File object to the Formatter constructor and it takes care of all the output work. If the file doesn't exist yet, then the Formatter will create it, though you will need to catch an exception that is thrown in such a case.

FormatWriteFileApp.java

import java.io.*;
import java.util.Formatter;

/**
  * Demonstrate the java.util.Formatter capabilities for
  * formatting primitive types and sending them to a file.
**/
public class FormatWriteFileApp
{
  public static void main (String arg[]) {
    Formatter formatter = null;
    File file = null;

    // Get the file from the argument line.
    if (arg.length > 0) file = new File (arg[0]);
    // Else use a default file name.
    if (file == null) {
      System.out.println ("Default: textOutput.txt");
      file = new File ("textOutput.txt");
    }

    // Send formatted output to the file.
    try {
      formatter = new Formatter (file);
    }
    catch  (FileNotFoundException e) {
      // File not found exception thrown since this is a new
      // file name. However, Formatter will create the new file.
    }

    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",   a_boolean);
    formatter.format ("byte    = %9d %n",   a_byte);
    formatter.format ("short   = %9d %n",   a_short);
    formatter.format ("int     = %9d %n",   an_int);
    formatter.format ("long    = %9d %n",   a_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 FormatWriteFileApp

This program creates a file that contains the same output as we obtained on the console with FormatWriteApp.

References and Web Resources

Latest update: Nov. 12, 2004

              Tech
Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
HistogramStream
  Demo 3
Filtering Data
  Demo 4
Exercises

           Physics
Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Analysis
Expt. + Analysis
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.