package agenda;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JFileChooser;


public class Io {

	public static void writeIo(Agenda agenda) throws IOException
	{
		JFileChooser fileChooser = new JFileChooser();
		if(fileChooser.showSaveDialog(null)
				== fileChooser.APPROVE_OPTION) {
			java.io.File file = fileChooser.getSelectedFile();

			ObjectOutputStream output = null;
			try{
				output = new ObjectOutputStream(new FileOutputStream(file));
			}catch(IOException e){ 
				System.out.println("Could not open file." + e);
			}
			try{
				output.writeObject(agenda);
				output.close();
			}catch(IOException e){
				System.out.println("Writing error.  " + e);
			}
		}

	}

	public static Agenda readIo() throws IOException
	{
		JFileChooser fileChooser = new JFileChooser();
		if(fileChooser.showOpenDialog(null)
				== fileChooser.APPROVE_OPTION) {
			java.io.File file = fileChooser.getSelectedFile();

			ObjectInputStream input = null;

			try{
				input = new ObjectInputStream(new FileInputStream(file));
			}catch(IOException e){
				System.out.println("There was a problem opening the file. " + e);

			}
			Agenda agenda = null;
			try{
				agenda = (Agenda)input.readObject();
				input.close();
				return agenda;
			}catch(Exception e){
				System.out.println("There was a issue reading this file: " + e);
			}
		}
		return null;


	}
}







