Advertisement
shadowsofme

Help With Saving

Jan 17th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. public void save (String fileName){
  2.     ChangeJar saveJar = new ChangeJar();
  3.  
  4.     // Saves the values of the current "ChangeJar" to "saveJar"
  5.     saveJar.quarters = quarters;
  6.     saveJar.dimes = dimes;
  7.     saveJar.nickels = nickels;
  8.     saveJar.pennies = pennies;
  9.  
  10.     try {
  11.         FileOutputStream fileOut = new FileOutputStream("/tmp/" + fileName + ".cjr");
  12.         ObjectOutputStream out = new ObjectOutputStream(fileOut);
  13.         out.writeObject(saveJar);
  14.         out.close();
  15.         fileOut.close();
  16.         System.out.printf("Saved ChangeJar is saved in /tmp/" + fileName + ".cjr");
  17.     }catch(IOException i) {
  18.         i.printStackTrace();
  19.     }
  20. }
  21.  
  22. public void load (String fileName){
  23.     try {
  24.         FileInputStream fileIn = new FileInputStream("/tmp/" + fileName + ".cjr");
  25.         ObjectInputStream in = new ObjectInputStream(fileIn);
  26.         ChangeJar loadJar = (ChangeJar) in.readObject();
  27.         in.close();
  28.         fileIn.close();
  29.     }catch(IOException i) {
  30.         i.printStackTrace();
  31.         return;
  32.     }catch(ClassNotFoundException c) {
  33.         System.out.println("ChangeJar class not found");
  34.         c.printStackTrace();
  35.         return;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement