Home : Course Map : Chapter 9 : Java :
Text Output to Files
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

We can use the FileWriter stream class to write to a file text data that consists of both strings and primitive type data. The following PrintWriterFileApp example gets the name for an output file either from a parameter on the command line or from a default.

The program follows closely to PrintWriterApp discussed in the Text Output to Console section except that instead of starting with the PrintStream object referenced by System.out, we create a FileWriter for a File object and then wrap this with a BufferedWriter and a PrintWriter. These two wrappers give us the efficiency of stream buffering and a handy set of print methods.

import java.io.*;

/**
  * Demonstrate wrapping streams to send text and primitive
  * type values to a file.
**/
public class PrintWriterFileApp
{
  public static void main (String arg[]) {

    File file = null;

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

    // Create a FileWriter stream to a file and then wrap
    // a PrintWriter object around the FileWriter to print
    // primitive values as text to the file.
    try {
        // Create a FileWriter stream to the file
        FileWriter file_writer = new FileWriter (file);

        // Put a buffered wrappter around it
        BufferedWriter buf_writer = new BufferedWriter (file_writer);

        // In turn, wrap the PrintWriter stream around this output stream
        // and turn on the autoflush.
        PrintWriter print_writer = new PrintWriter (buf_writer,true);

        // Use the PrintWriter println methods to send strings and
        // primitives to the file.
        print_writer.println ("Text output with PrintWriter.");
        print_writer.println ("Primitives converted to strings:");

        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;

        print_writer.println (a_boolean);
        print_writer.println (a_byte);
        print_writer.println (a_short);
        print_writer.println (an_int);
        print_writer.println (a_long);
        print_writer.println (a_float);
        print_writer.println (a_double);

        // PrintWriter doesn't throw IOExceptions but instead
        // offers the catchError() method
        if (print_writer.checkError ()) {
            System.out.println ("An output error occurred!" );
        }

    }
    catch (IOException ioe){
      System.out.println("IO Exception");
    }
  } // main

} // class PrintWriterFileApp

The output from this program is the same as for PrintWriterApp except that instead of going to the console it goes to a file.

References & 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.