I created a rudimentary game with several objects. I would like to give the user of the program the ability to save and load the state of the program.
I researched several articles and read many overflow posts. I was surprised at the number of methods and the complexity of each of these methods.
Most methods require you to create a framework or skeleton of all the objects that I want to save, and then load them one by one, calling each object manually and then returning it back to its type and data class.
Yes, I am new to programming. But I do not ask for distribution. I ask for a brief explanation.
I read the following article as I explored the idea of outputting to an XML file.
http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/
As I said, I am new to programming. Should I learn XML in tandem with Java?
My question is this:
A) Does my IDE, when it is successfully compiled, not know how many objects I have, their data type and what class they belong to?
B) Is there a way to save ALL objects without specifying them separately.
c) I overloaded the complexity of maintaining a simple program?
Should I learn XML in tandem with Java?
There is no reason if you do not want to learn XML or think XML is a good option for your application. There are many ways to serialize without XML.
Does my IDE, when successfully compiling, not know how many objects I have, their data type and what class they belong to?
, . Java, , .
Foo[] foos = new Foo[ new Scanner(System.in).nextInt() ]; for(int i = 0; i < foos.length; ++i) foos[i] = new Foo();
( Foo ? .)
Foo
, .
B) , .
, Serializable. . , .
class FooState implements Serializable { Integer a, b, c; // Integer and String String d, e, f; // also implement Serializable } class Foo { static FooState allMyState = new FooState(); public static void main(String[] args) throws IOException { try(ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(new File("myFile.data")))) { // no need to specify members individually oos.writeObject(allMyState); } } }
, , , JSON, XML ..
, , .
c) ?
. , .
A) IDE , , ?
IDE . , , , .
, , - , , , , . , , XML, JSON ( ).
, , JVM . , - , JVM ( Just-In-Time, " " ). . , - . , , . , .
C) ?
, Game Game(String name) ( ). , new Game("firstGame"). . , , . , , , . , , JSON JSON (, Jackson).
Game
Game(String name)
new Game("firstGame")
public class Game{ int coOrdinateX; int coOrdinateY; int score; String[] powersCurrentlyTheUserHad; ... }
, . , , .
Serialization is a way of storing objects on disk (essentially files), so when de-serializing using library functions, you immediately return the object with the current state.
check out http://www.tutorialspoint.com/java/java_serialization.htm for more details.