Activity 4.3 - Writing a date-stamp to a text file

Topic

In this activity, we write a date-stamp to a text file.

Materials

In addition to the Solution document, the project for this activity contains three classes:

In addition, the project folder contains a text file, sample2.txt, empty at present, which is to be date-stamped.

Task

At present the run method in DateStamper merely obtains a date-stamp and prints an informative message via System.out. Nothing is actually written to the file as yet. Your task is to supply the code that opens an output stream for writing to the text file, and writes the date-stamp to the file.

Instructions

Open the file sample2.txt, using the File|Open File... option from the IDE. It should be empty. Leave the file open.

Open DateStamper and insert code where indicated. Run the project.

If your code is correct, you should see that sample2.txt has now acquired a date-stamp.

Notes

  1. For simplicity, this application uses only a FileWriter and a PrinterWriter, with no BufferedWriter between them.
  2. If you edit the Main class to alter the file name passed to the run method, and then re-run the project, you will find that a new file of that name is created and date-stamped.