Advertisement
joxaren

DataSavingTest (with GameCharacter)

Oct 3rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. public class DataSavingTest {
  5.  
  6.     static int ObjectsSaved;
  7.  
  8.     public static void main(String[] args) {
  9.         GameCharacter one = new GameCharacter(50, "Elf", new String[]{"bow", "sword", "dust"});
  10.         GameCharacter two = new GameCharacter(200, "Troll", new String[]{"bare hands", "big ax"});
  11.         GameCharacter three = new GameCharacter(120, "Magician", new String[]{"spells", "invisibility"});
  12.  
  13.         saveCharacterData(one, two, three); // testing
  14.         ArrayList<GameCharacter> characterList = retrieveCharacterData(); // testing
  15.         System.out.println(characterList.get(0).getType()); // testing
  16.         System.out.println(characterList.get(1).getType()); // testing
  17.         System.out.println(characterList.get(2).getType()); // testing
  18.     }
  19.  
  20.     public static void saveCharacterData(Object... objects) {
  21.         try {
  22.             ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Save.txt"));
  23.             for (Object o : objects) {
  24.                 os.writeObject(o);
  25.                 ObjectsSaved++;
  26.             }
  27.             os.close();
  28.         } catch (FileNotFoundException ex) {
  29.             ex.printStackTrace();
  30.             System.out.println("Save file not found.");
  31.         } catch (IOException ex) {
  32.             ex.printStackTrace();
  33.         }
  34.     }
  35.  
  36.     public static ArrayList<GameCharacter> retrieveCharacterData() {
  37.         ArrayList<GameCharacter> characterList = new ArrayList<>();
  38.         try {
  39.             ObjectInputStream is = new ObjectInputStream(new FileInputStream("Save.txt"));
  40.             for (int i = 0; i < ObjectsSaved; i++) {
  41.                 GameCharacter character = (GameCharacter) is.readObject();
  42.                 characterList.add(character);
  43.             }
  44.             is.close();
  45.         } catch (FileNotFoundException ex) {
  46.             ex.printStackTrace();
  47.             System.out.println("Save file not found.");
  48.         } catch (ClassNotFoundException ex) {
  49.             ex.printStackTrace();
  50.         } catch (IOException ex) {
  51.             ex.printStackTrace();
  52.         }
  53.         return characterList;
  54.     }
  55. }
  56. _________________________________
  57.  
  58. import java.io.*;
  59.  
  60. public class GameCharacter implements Serializable {
  61.     int power;
  62.     String type;
  63.     String[] weapons;
  64.  
  65.     public GameCharacter(int p, String t, String[] w) {
  66.         power = p;
  67.         type = t;
  68.         weapons = w;
  69.     }
  70.  
  71.     public int getPower() {
  72.         return power;
  73.     }
  74.  
  75.     public String getType() {
  76.         return type;
  77.     }
  78.  
  79.     public String getWeapons() {
  80.         String weaponList = "";
  81.         for (int i = 0; i < weapons.length; i++) {
  82.             weaponList += weapons[i] + " ";
  83.         }
  84.         return weaponList;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement