eMails 2 - Saving to a Disk File

  Download Source Code

/**************************************************
Emalia has written a program to collect eMail addresses into
an array. Now she has improved the program to save the array
in a file on the disk drive. When the program starts,
it automatically loads the file's contents into the array.
When the program ends, it automatically saves the contents
of the array back into the disk file. Now her data is PERSISTENT.
***************************************************/


import java.io.*; public class EmailsSaved { String[] addresses = new String[1000]; int count = 0; // the number of addresses in the array public EmailsSaved() { loadArray(); String email=""; do { printAllAddresses(); email = input("Type an eMail address (ENTER to quit)"); if( isValid(email) ) { addToAddresses(email); saveArray(); } else { output(email + " is not valid"); } } while(email.length() > 0); // type nothing to quit } boolean isValid(String email) // check whether email is valid { if(email.length() < 5) // shortest is a@b.c { return false; } int atsign = email.indexOf("@"); // find the @ sign if(atsign < 0 ) { return false; } // @ is missing String server = email.substring(atsign+1); // the rest after @ int dot = server.indexOf("."); // find . in server if( dot < 0) { return false; } // . is missing return true; // if it got this far, it's valid } void addToAddresses(String email) // pre-condition : COUNT contains number of items in the array // post-condition: COUNT is 1 bigger and addresses[count] == email { addresses[count] = email; count = count + 1; } void printAllAddresses() { for(int c=0; c < count; c = c+1) { System.out.println(addresses[c]); } System.out.println("============"); } void saveArray() { try { ObjectOutputStream os=new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("addresses.dat"))); os.writeObject(addresses); os.close(); } catch(Exception e) { System.out.println(e.toString()); } } void loadArray() { try { ObjectInputStream os=new ObjectInputStream( new FileInputStream("addresses.dat")); addresses = (String[])os.readObject(); os.close(); } catch(Exception e) { System.out.println(e.toString()); } int p = -1; do { p = p+1; } while(addresses[p]!=null); count = p; } public static void main(String[] args) { new EmailsSaved(); } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } public void output(String message) { javax.swing.JOptionPane.showMessageDialog(null,message); } }

Persistent Storage in Files

The original email collection program saved the addresses in an array.  When the program ended, all the addresses
disappeared, because the array is stored in RAM and that is erased whenever a program ends or the computer shuts down.
This program is much better.  Each time an address is added, the program copies the entire array into a text-file
stored on the disk drive.  When the program starts the next time, it automatically loads the contents of the text-file
into the array.

This persistent storage is achieved by object serialization.  The relevant Java commands are contained in the
methods loadArray( ) and  storeArray( ) . An explanation of these commands is beyond the scope of this course.
But they do work correctly.

There are other ways to store data and retrieve it from a data file - these will be explored in later examples.

Progamming Practice