SimIo.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package gui.simulator;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import javax.swing.JFileChooser;
  8. import javax.swing.JPanel;
  9. public class SimIo {
  10. public static void writeIo(JPanel panel) throws IOException
  11. {
  12. JFileChooser fileChooser = new JFileChooser();
  13. if(fileChooser.showSaveDialog(null)
  14. == fileChooser.APPROVE_OPTION) {
  15. java.io.File file = fileChooser.getSelectedFile();
  16. ObjectOutputStream output = null;
  17. try{
  18. output = new ObjectOutputStream(new FileOutputStream(file));
  19. }catch(IOException e){
  20. System.out.println("Could not open file." + e);
  21. }
  22. try{
  23. output.writeObject(panel);
  24. output.close();
  25. }catch(IOException e){
  26. System.out.println("Writing error. " + e);
  27. }
  28. }
  29. }
  30. public static SimulatorPane readIo() throws IOException
  31. {
  32. JFileChooser fileChooser = new JFileChooser();
  33. if(fileChooser.showOpenDialog(null)
  34. == fileChooser.APPROVE_OPTION) {
  35. java.io.File file = fileChooser.getSelectedFile();
  36. ObjectInputStream input = null;
  37. try{
  38. input = new ObjectInputStream(new FileInputStream(file));
  39. }catch(IOException e){
  40. System.out.println("There was a problem opening the file. " + e);
  41. }
  42. SimulatorPane pane = null;
  43. try{
  44. pane = (SimulatorPane)input.readObject();
  45. input.close();
  46. return pane;
  47. }catch(Exception e){
  48. System.out.println("There was a issue reading this file: " + e);
  49. }
  50. }
  51. return null;
  52. }
  53. }