You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

  1. package playground;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.util.ArrayList;
  9. import org.apache.logging.log4j.Logger;
  10. import org.apache.logging.log4j.LogManager;
  11. public class SaveGame {
  12. private static String datnam = "aktuellerSpielzustand.ser";
  13. private static Logger logger = LogManager.getLogger(SaveGame.class);
  14. @SuppressWarnings("unchecked")
  15. public static void save() {
  16. // gameobjects.AnimatedGameobject
  17. ArrayList<String> objArrList = null;
  18. ObjectInputStream in = null;
  19. try {
  20. in = new ObjectInputStream(new FileInputStream(datnam));
  21. objArrList = (ArrayList<String>) in.readObject();
  22. } catch (FileNotFoundException ex) {
  23. logger.warn("Savegame file not (yet) existing!");
  24. } catch (Exception ex) {
  25. logger.error(ex);
  26. } finally {
  27. try {
  28. if (in != null)
  29. in.close();
  30. } catch (IOException e) {
  31. }
  32. }
  33. if (objArrList == null)
  34. objArrList = new ArrayList<String>();
  35. objArrList.add(new String("ArrayListgroesse: " + objArrList.size()));
  36. logger.debug(objArrList);
  37. ObjectOutputStream aus = null;
  38. try {
  39. aus = new ObjectOutputStream(new FileOutputStream(datnam));
  40. aus.writeObject(objArrList);
  41. } catch (IOException ex) {
  42. logger.error(ex);
  43. } finally {
  44. try {
  45. if (aus != null) {
  46. aus.flush();
  47. aus.close();
  48. }
  49. } catch (IOException e) {
  50. }
  51. }
  52. }
  53. }