Writing, Rewriting, and Rerewriting the Same Java Program

By Barry A. Burd

Have you ever asked yourself what would happen if you ran the same file writing program more than once in Java. Create the tiny program below and run the program twice. Then examine the program’s output file. The output file contains only two letters.

java.io file
Testing the import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintStream;

 

class WriteOK {

 

public static void main(String args[])throws FileNotFoundException {

 

PrintStream diskWriter = new PrintStream(new File("approval.txt"));

 

diskWriter.print ('O');

diskWriter.println('K');

 

diskWriter.close();

}

Here’s the sequence of events, from the start to the end of the experiment:

  1. Before you run the code, the computer’s hard drive has no approval.txt file. 

    That’s okay. Every experiment has to start somewhere.

  2. Run the code above. 

    The call to new PrintStream creates a file named approval.txt. Initially, the new approval.txt file contains no characters. Later in the code, calls to print and println put characters in the file. So, after running the code, the approval.txt file contains two letters: the letters OK.

  3. Run the code a second time.
    At this point, you could imagine seeing OKOK in the approval.txt file. But that’s not what you see above. After running the code twice, the approval.txt file contains just one OK. Here’s why:

    • The call to new PrintStream in the code deletes the existing approval.txt file. The call creates a new, empty approval.txt file.
    • After a new approval.txt file is created, the print method call drops the letter O into the new file.
    • The println method call adds the letter K to the same approval.txt file.

That’s the story. Each time you run the program, it trashes whatever approval.txt file is already on the hard drive. Then the program adds data to a newly created approval.txt file.

Where’s my file?

Create an Eclipse project containing the following code:

import java.util.Scanner;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintStream;

 

class ReadAndWrite {

 

public static void main(String args[]) throws FileNotFoundException {

Scanner diskScanner = new Scanner(new File("data.txt"));

PrintStream diskWriter = new PrintStream("data.txt");

 

diskWriter.println("Hello");

 

System.out.println(diskScanner.next());

 

diskScanner.close();

diskWriter.close();

}

}

When you run the code, you see an error message in Eclipse’s Console view. Why?

Write and then read

Modify the code from the where’s-my-file experiment so that the code>PrintStream diskWriter declaration comes before the Scanner diskScanner declaration.

When you run the code, the word Hello should appear in Eclipse’s Console view. After running the code, check to make sure that your Eclipse project contains a file named data.txt.

Random numbers in a file

Create a program that writes ten randomly generated numbers in a disk file. After writing the numbers, the program reads the numbers from the file and displays them in Eclipse’s Console view.