Advertisement
Guest User

Save.java

a guest
Apr 1st, 2015
1,337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package me.joejoe.rpg;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8.  
  9. public class Save {
  10.  
  11.     int[] saveInfo = { Game.hp, Game.level, Game.mana, Game.expTotal,
  12.             Game.goldTotal, Game.arrow, Game.shuriken, Game.bomb,
  13.             Game.hpPotion, Game.mpPotion, Game.potion, Game.items };
  14.  
  15.     public Save() {
  16.         readPlayer("saveFile.txt");
  17.         savePlayer("saveFile.txt");
  18.     }
  19.  
  20.     private void readPlayer(String filePath) {
  21.         File inputFile;
  22.         BufferedReader inputReader;
  23.  
  24.         try {
  25.             inputFile = new File(filePath);
  26.             inputReader = new BufferedReader(new FileReader(inputFile));
  27.  
  28.             Game.hp = saveInfo[0];
  29.             Game.level = saveInfo[1];
  30.             Game.mana = saveInfo[2];
  31.             Game.expTotal = saveInfo[3];
  32.             Game.goldTotal = saveInfo[4];
  33.             Game.arrow = saveInfo[5];
  34.             Game.shuriken = saveInfo[6];
  35.             Game.bomb = saveInfo[7];
  36.             Game.hpPotion = saveInfo[8];
  37.             Game.mpPotion = saveInfo[9];
  38.             Game.potion = saveInfo[10];
  39.             Game.items = saveInfo[11];
  40.            
  41.             for (int i = 0; i < saveInfo.length; i++) {
  42.                 saveInfo[i] = Integer.parseInt(inputReader.readLine());
  43.             }
  44.  
  45.             inputReader.close();
  46.  
  47.         } catch (Exception e) {
  48.             e.printStackTrace();
  49.         }
  50.  
  51.     }
  52.  
  53.     private void savePlayer(String filePath) {
  54.         File outputFile;
  55.         BufferedWriter outputWriter;
  56.        
  57.         try {
  58.             outputFile = new File(filePath);
  59.            
  60.             outputWriter = new BufferedWriter(new FileWriter(outputFile));
  61.            
  62.             saveInfo[0] = Game.hp;
  63.             saveInfo[1] = Game.level;
  64.             saveInfo[2] = Game.mana;
  65.             saveInfo[3] = Game.expTotal;
  66.             saveInfo[4] = Game.goldTotal;
  67.             saveInfo[5] = Game.arrow;
  68.             saveInfo[6] = Game.shuriken;
  69.             saveInfo[7] = Game.bomb;
  70.             saveInfo[8] = Game.hpPotion;
  71.             saveInfo[9] = Game.mpPotion;
  72.             saveInfo[10] = Game.potion;
  73.             saveInfo[11] = Game.items;
  74.            
  75.             for(int i = 0; i < saveInfo.length; i++){
  76.                 outputWriter.write(Integer.toString(saveInfo[i]) + "\n");
  77.             }
  78.             outputWriter.close();
  79.            
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement