Reputation: 3257
I know that using File class
,I can store data stored in variables on hard disk and retrieve them later.
But is there any way that i can store an object from a class that has some variables
and methods and use the object later on .
let's say classes ClassA and ClassB are two classes of a game:
public class classA{
public int x,y,Vx,Vy ;
public void move(){
x +=Vx ;
y +=Vy ;
} ... }
public claassB{
classA c = new classA();
while(1){
c.move() ;
}
}
now let's say that i click save button and close the game and i rerun and load the game
by clicking the load button .
so is there any way that i can store "c" so when i load the game . the stored object would be retrieved and the game would be continued from where i left off. actually instead of storing the variables of the object i want to store the object . so i can pass the object to classB (after the load button is clicked) .
Upvotes: 0
Views: 880
Reputation: 2841
You can use serialization to serialize your object- Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. Here is a nice example.
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
And here shows how to use:
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan, Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
Check documentation to get more informations.Maybe you find that serialization is the right way to go in your case
Source of example: Tutorialspoint
Upvotes: 3
Reputation: 2482
public final static void writeObject(Object x,String name) throws IOException{
try
{
FileOutputStream fileOut = new FileOutputStream(name+".ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(x);
out.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}}
public final static Object readObject(String filename) throws IOException, ClassNotFoundException{
ArrayList oldlist = null;
try
{
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fileIn);
oldlist = (ArrayList) in.readObject();
in.close();
fileIn.close();
return oldlist;
}catch(IOException i)
{
writeObject(list, "list");
update_list(current_link);
System.exit(0);
//i.printStackTrace();
return 0;
}catch(ClassNotFoundException c)
{
c.printStackTrace();
return null;
}}
Upvotes: 0
Reputation: 1
I think what you mean is that you have a class C that contains variables and methods, you then want to store all of this on the hard drive so you can retrieve all of it later on.
Then you should look into the consept of Serializable
http://en.wikipedia.org/wiki/Serialization#Java
Upvotes: 0
Reputation: 3222
I think you're looking for Serializable, ObjectOutputStream, and ObjectInputStream.
Upvotes: 1