Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
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. file
Testing the import;



class WriteOK {

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

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

diskWriter.print ('O');




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;




class ReadAndWrite {

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

<strong> Scanner diskScanner = new Scanner(new File("data.txt"));</strong>

<strong> PrintStream diskWriter = new PrintStream("data.txt");</strong>







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.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: