Io.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package agenda;
  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. public class Io {
  9. public static void writeIo(Agenda agenda) throws IOException
  10. {
  11. JFileChooser fileChooser = new JFileChooser();
  12. if(fileChooser.showSaveDialog(null)
  13. == fileChooser.APPROVE_OPTION) {
  14. java.io.File file = fileChooser.getSelectedFile();
  15. ObjectOutputStream output = null;
  16. try{
  17. output = new ObjectOutputStream(new FileOutputStream(file));
  18. }catch(IOException e){
  19. System.out.println("Could not open file." + e);
  20. }
  21. try{
  22. output.writeObject(agenda);
  23. output.close();
  24. }catch(IOException e){
  25. System.out.println("Writing error. " + e);
  26. }
  27. }
  28. }
  29. public static Agenda readIo() throws IOException
  30. {
  31. JFileChooser fileChooser = new JFileChooser();
  32. if(fileChooser.showOpenDialog(null)
  33. == fileChooser.APPROVE_OPTION) {
  34. java.io.File file = fileChooser.getSelectedFile();
  35. ObjectInputStream input = null;
  36. try{
  37. input = new ObjectInputStream(new FileInputStream(file));
  38. }catch(IOException e){
  39. System.out.println("There was a problem opening the file. " + e);
  40. }
  41. Agenda agenda = null;
  42. try{
  43. agenda = (Agenda)input.readObject();
  44. input.close();
  45. return agenda;
  46. }catch(Exception e){
  47. System.out.println("There was a issue reading this file: " + e);
  48. }
  49. }
  50. return null;
  51. }
  52. }