I am working on my project for intro to programming. The program is supposed to use a text file as a database to store first names, last names, and phone numbers. The program needs to be able to manipulate the data on the text file.

[Instructions](https://i.stack.imgur.com/ixXZ4.png)

Here is my entire code so far: 

```
import java.util.*;
import java.io.*;
import java.lang.*;
import java.nio.*;

public class Project
{
private static Scanner input;
private static Formatter x;

public static void main(String[] args) 
{
    String choice = "";
    Scanner userInput = new Scanner(System.in);
    System.out.printf("Please choose one of the following commands:%nL (Listing), I (insert), S (search), D (delete)%nM (modify), W (write), Q (quit with saving)");

    loop: while (choice != "Q") {
        System.out.printf("%nEnter your Command: ");
        choice = userInput.nextLine();

        switch (choice)
        {
            case "L":
                Listing();
                break;

            case "I":
                Insert();
                break;

            case "S":
                break;

            case "D":
                break;

            case "M":
                break;

            case "W":
                break;

            case "Q":
                break loop;
        } // end switch statement
    } // end while loop
} // end method main

public static void Listing() // List records from MiniDB.txt file
{
    try {
            File file = new File("MiniDB.txt");
            input = new Scanner(file);
        } 
    catch (IOException ioException) {
     System.err.println("Cannot open file.");
     System.exit(1);
        }
        if (input.hasNext()) { // If there are records in the file print them, else print none found
            while (input.hasNext()) // while there is more to read
            {
                String firstName = input.next();
                String lastName = input.next();
                String phoneNumber = input.next();

            System.out.printf("%-13s%-13s%s%n", firstName + ",", lastName, phoneNumber); }
            }
        else {
            System.out.printf("No records found");
        }
} // end method Listing

public static void Insert() // insert a record
{
    try {
            String file = "MiniDB.txt";
            input = new Scanner(System.in);
            PrintWriter outputStream = new PrintWriter(new FileWriter(file, true) );

            System.out.printf("Last Name: ");
            String lastName = input.next();
            System.out.printf("%nFirst Name: ");
            String firstName = input.next();
            System.out.printf("%nTelephone Number: ");
            String phoneNumber = input.next();

            outputStream.printf("%n%s %s %s", firstName, lastName, phoneNumber);
            outputStream.close();
            System.out.printf("%nDone.");
    } 
    catch (IOException ioException) {
     System.err.println("Cannot open file.");
     System.exit(1);
    }
} // end method Insert

public static void Search(); // search a record
{

}
} // end class Project

```

What I need help with is understanding how to create the "write" command. Currently, my code makes the changes to the text file automatically, but I need it to finalize changes only when W is inputted. I've been reading for awhile on this and I'm completely stuck. Do I need to rewrite what I have?

I was thinking about making all the changes on a temp.txt file and then if W is inputted, renaming temp.txt MiniDB.txt and overwriting that file. I would then need to delete the temp.txt file. I feel like there should be an easier way?