Java. Is there any way to save/load whole game/app using only one java file(class...)?

Salmeh

I don't know is there any way in java to save & load the whole game/app using only one script(java file,class...)?

For example if I create many int's and string's in lots of java files, Is there any way to save them all using only one script. And when I turn off the game/app (System.exit(1);) and when I go back to game it will load by itself (I won't lose my stats.).

Is there any way to do that?

Sweeper

You should not save your game data to a Java file. It makes no sense. Java files are meant to store Java source code, not your game data.

You should write your game data to some other files and read it when the program starts.

You basically use ObjectOutputStream and ObjectInputStream to do this.

Just before the user closes the program, you create a new ObjectOutputStream:

FileOutputStream fos = new FileOutputStream("The/Path/That/You/Want/The/Data/File/To/be");
ObjectOutputStream stream = new ObjectOutputStream(fos);

Then you put all your game data (your ints and strings) into one single POJO that implements Serializable. After that, write this POJO to that file.

This is the POJO class:

public class GameData implements Serializable {
    private int int1, int2, int3;
    private String s1, s2, s3;

    // getters for the fields

    public GameData(int int1, int int2, int int3, String s1, String s2, String s3) {
        this.int1 = int1;
        this.int2 = int2;
        this.int3 = int3;
        this.s1 = s1;
        this.s2 = s2;
        this.s2 = s3;
    }
}

And you save the data like this:

GameData data = new GameData(someInt1, someInt2, someInt3, someStr1, someStr2, someStr3);
stream.writeObject(data);

When you want to read the data back, just create a FileInputStream and an ObjectInputStream and cast the read object to GameData and you can get all your ints and strings back!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSON parsing only one branch of whole message java using GsonBuilder and InputStream Android

From Dev

is there any performance impacts of using whole methods as public in java?

From Dev

is there any performance impacts of using whole methods as public in java?

From Dev

Iterating only one xml tag using Java

From Dev

Java parallel stream using only one thread?

From Dev

Is there any way to generate a UUID in Java that is identical to that of the one generated in C#?

From Java

Is there any way to draw border around 'whole' and 'only' span in javascript?

From Dev

Is there a way to replace only one captured group in java matcher regex?

From Dev

Is there a way, I can empty the whole JSONObject -- java

From Dev

Is there any way of reindent the WHOLE code without using external codeblocks plugins?

From Java

Is there any way to add a "button" to the Windows Notification Area using Java

From Dev

Is there any way to create zip in a zip file using java dynamically..?

From Dev

is there any way to read rtf file using java other than RTFEditorKit?

From Dev

Is there any way to draw graphics on a JPanel using Thread in java?

From Dev

Any way of mapping java servlet using simple regex?

From Dev

Is there any way to create zip in a zip file using java dynamically..?

From Dev

Java regex: can I match groups using only one regex?

From Dev

Error using decimals only on one system. Java

From Dev

How to retrieve only one column from collection in mongodb using java?

From Dev

Using Java's replaceAll to replace the whole string

From Dev

Is there any easy way to rotate an image about z axis using java without jumping into java 3d?

From Dev

Is there any way in java to limit a package's visibility to only some other package

From Dev

Is there any way for reading two or more files in one Java8-stream?

From Dev

if condition to check if only one variable is set of the four variables at any point in JAVA

From Dev

Is there any way to assemble only one flavor of the androidTest builds?

From Dev

Is there any way to match a string with only one character into character 'X' in sed?

From Dev

Is there any way to match a string with only one character into character 'X' in sed?

From Dev

Is there any way to rename or hide only one HTML tag?

From Dev

Kill only one Java process

Related Related

  1. 1

    JSON parsing only one branch of whole message java using GsonBuilder and InputStream Android

  2. 2

    is there any performance impacts of using whole methods as public in java?

  3. 3

    is there any performance impacts of using whole methods as public in java?

  4. 4

    Iterating only one xml tag using Java

  5. 5

    Java parallel stream using only one thread?

  6. 6

    Is there any way to generate a UUID in Java that is identical to that of the one generated in C#?

  7. 7

    Is there any way to draw border around 'whole' and 'only' span in javascript?

  8. 8

    Is there a way to replace only one captured group in java matcher regex?

  9. 9

    Is there a way, I can empty the whole JSONObject -- java

  10. 10

    Is there any way of reindent the WHOLE code without using external codeblocks plugins?

  11. 11

    Is there any way to add a "button" to the Windows Notification Area using Java

  12. 12

    Is there any way to create zip in a zip file using java dynamically..?

  13. 13

    is there any way to read rtf file using java other than RTFEditorKit?

  14. 14

    Is there any way to draw graphics on a JPanel using Thread in java?

  15. 15

    Any way of mapping java servlet using simple regex?

  16. 16

    Is there any way to create zip in a zip file using java dynamically..?

  17. 17

    Java regex: can I match groups using only one regex?

  18. 18

    Error using decimals only on one system. Java

  19. 19

    How to retrieve only one column from collection in mongodb using java?

  20. 20

    Using Java's replaceAll to replace the whole string

  21. 21

    Is there any easy way to rotate an image about z axis using java without jumping into java 3d?

  22. 22

    Is there any way in java to limit a package's visibility to only some other package

  23. 23

    Is there any way for reading two or more files in one Java8-stream?

  24. 24

    if condition to check if only one variable is set of the four variables at any point in JAVA

  25. 25

    Is there any way to assemble only one flavor of the androidTest builds?

  26. 26

    Is there any way to match a string with only one character into character 'X' in sed?

  27. 27

    Is there any way to match a string with only one character into character 'X' in sed?

  28. 28

    Is there any way to rename or hide only one HTML tag?

  29. 29

    Kill only one Java process

HotTag

Archive