Sie sind auf Seite 1von 2

25-1

Lesson 25..Writing to a Text File


Making preparations:
Writing to a text file is very simple. We will again need to do two things we are already
accustomed to doing when reading text files:
1. java.io.* must be imported.
2. Use throws IOException as part of the signature of the method containing our file
output code.
Create FileWriter and PrintWriter objects:
FileWriter fw = new FileWriter(C:\\temp_Name\\Output1.out );
Notice here that we specify the name of the file we wish to create. This object
sends one character at a time to the file. This could be a bit inconvenient. For
example, if we need to output Hello good buddy, we would need to output all
16 characters separately.
PrintWriter output = new PrintWriter(fw);
This final object, output, permits us to use a single command to write entire
sentences (or numbers) to the file. This PrintWriter class has two methods of
which we need to be aware.
a. print( )
b. println( )
print( ) and println( ) are used in exactly the same way in which they are used
with System.out.
Complete class in which we write to a file:
import java.io.*;
public class WriteToFile
{
public static void main(String args[]) throws IOException
{
FileWriter fw = new FileWriter(C:\\temp_Name\\Output1.out);
PrintWriter output = new PrintWriter(fw);
output.print(Four-score and );
double d = 7.023;
output.println(d);
output.println(years ago.);
output.close( ); //These two lines are very important. Some of the data
fw.close( );
//may not actually be put on disk until you close.
}
}
Load Notepad and look at the file Output1.out. The following is what you should see.

25-2
Four-score and 7.023
years ago.

Project. Write Student Averages


Modify the project (Determining Student Averages) from Lesson 24 so that it will print the
output to a file rather than a console screen. Your output file should be stored in your standard
folder, temp_Name and the file name should be StudentScores.out. At the completion of the
program, the contents of StudentScores.out should be:
Agnes, average =
Bufford, average
Julie, average =
Alice, average =
Bobby, average = 93

76
= 91
94
39

Call this new class StudentAverages_Out.

****************************************************************************
Appending to a file:
Occasionally it may be desirable to append new content to the end of an existing file
rather that overwriting it as is the case with all of the previous code in this lesson. To
accomplish this, just make the following modification to the creation of the FileWriter
object:
FileWriter fw = new FileWriter(C:\\temp_Name\\Output1.out, true);
The new parameter, true, simply says, Yes, we want to append. If the file does not
already exist, the append mode will create and write to it.
Flushing the buffer:
The PrintWriter constructor will also accept a second parameter that indicates if we wish
to flush the buffer after each println. This forces storage to the disk at that moment
rather than waiting for the close method. This second parameter is not necessary if the
close method is issued at the end of output to the disk. The syntax for this is:
PrintWriter output = new PrintWriter(fw, true);

As an enrichment activity, take a look at Appendix F. There, you will learn the difference
between text and binary files.