
package gui.simulator;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFileChooser;


public class SimIo {

	public static void writeIo(List<Object> list) 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(list);
				output.close();
			}catch(IOException e){
				System.out.println("Writing error.  " + e);
			}
		}

	}

	public static List<Object> readIo() throws IOException
	{
		List<Object> objectlist = new ArrayList<Object>();

		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);

			}
			objectlist = null;
			try{

				objectlist = (List<Object>) input.readObject();

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


	}
}







