I have a program in which I need to store a Class object into memory by casting it into String. Is it possible to convert the String back into the original Class so that I can use that class variables? I am doing this in JAVA.
Example: test.java
class hello{
public String h1;
public String h2;
}
public class test {
public static void main(String[] args)
{
hello h = new hello();
h.h1 = "hello";
h.h2 = "world";
String s = h.toString();
System.out.println("Print s : "+s);
// Now I need to convert String s into type hello so that
// I can do this:
// (hello)s.h1;
// (hello)s.h2;
}
}
NOTE: this is not a homework, this is a personal project and I would be grateful if anyone can help!
Thanks! Iv开发者_运维百科ar
This could be helpful:
http://www.javabeginner.com/uncategorized/java-serialization
http://java.sun.com/j2se/1.4.2/docs/api/java/io/Serializable.html
EDIT added
toString()
is not the same thing as Serialization. That is merely a description of the Class; listing some values of that particular instance of the Class etc.
I think what you want to do is Serialization. I'm confused by your comment:
// Now I need to convert String s into type hello so that
// I can do this:
// (hello)s.h1;
// (hello)s.h2;
You can't just cast String objects to arbitrary class types. Maybe you can elaborate on what you're trying to accomplish here. If you want to be able to "save" a class to a file, then read it back in as an object, you want Serialization. Like this:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Hello implements Serializable {
public String h1;
public String h2;
@Override
public String toString() {
return "{ h1: " + h1 + ", h2: " + h2 + " }";
}
public static void main(String[] args) throws Exception {
Hello h = new Hello();
h.h1 = "hello";
h.h2 = "world";
ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream("hello.ser"));
outstream.writeObject(h);
System.out.println("1) h: " + h);
h = null;
System.out.println("2) h: " + h);
ObjectInputStream instream = new ObjectInputStream(new FileInputStream("hello.ser"));
h = (Hello) instream.readObject();
System.out.println("3) h: " + h);
}
}
It can get more complicated when your fields are more complex classes than String. Implementing Serializable is just a "marker" interface that says that the object can be serialized, it doesn't require any methods to be implemented. Your simple class just needs to be written out using an ObjectOutputStream and can be read back in using an ObjectInputStream.
I think what you need to do is to look into serialisation/de-serialisation.
Serialization and deserialization:
http://en.wikipedia.org/wiki/Serialization
If you make a copy of your object hello then you don't have to worry about changing back to the original object.
Hello h = new Hello();
Hello copyH = h;
I have a program in which I need to store a Class object into memory by
After creating an object it's already located in memory, if you want to cache it you may put into a HashMap for later access.
casting it into String.
There is no casting from String to Object and vice versa.
Is it possible to convert the String back into the original Class so that I can use that class variables?
Java Serialization is binary. To convert an objects state to a String you have several options.
Print the members to a String and use StringTokenizer to split the String and assign (which may require conversions for datatypes other than String)
Use something like libcsv
Use XML as a String representation of your object's state. In java you could use JAXB which I would prefer.
You could use the JavaBeans mechanisms and implement a PropertyEditor. You still need to implement the string-to-object parsing yourself and if your object is complex, you will need a lot of logic in your PropertyEditor to be able to parse the whole String. However, when you have the editors for the simple types ready, it is easy to utilize these in the more comlex editors.
Of course, if you don't absolutely need String representations of your objects, you should look into serialization as others have suggested.
If you really need to do something like that you can override the toString() method in your class and create a static method (or create a converter class) that returns a Hello object from it's toString(). Something like this:
class Hello() {
public String h1;
public String h2;
public String toString() {
return "h1:" + h1 +", h2:" + h2;
}
public static Hello fromString(String input) {
Hello hello = new Hello();
hello.h1 = // Some logic to extract h1 from input
hello.h2 = // Some logic to extract h2 from input
return hello;
}
}
That way you should be able to:
Hello hello = new Hello();
String helloString = hello.toString();
Hello newHello = Hello.fromString(helloString);
I encourage you to check Serialization (as many others had) or find what's your real problem and the best way to solve it.
精彩评论