You cannot store a class object into a string using toString(), toString() only returns a String representation of your object-in any way you'd like. You might want to do some reading about Serialization.
Much easier way of doing it: you will need com.google.gson.Gson for converting the object to json string for streaming
to convert object to json string for streaming use below code
Gson gson = new Gson();
String jsonString = gson.toJson(MyObject);
To convert back the json string to object use below code:
Gson gson = new Gson();
MyObject = gson.fromJson(decodedString , MyObjectClass.class);
Much easier way to convert object for streaming and read on the other side. Hope this helps. - Vishesh
Continuing from my comment. toString
is not the solution. Some good soul has written whole code for serialization and deserialization of an object in Java. See here: http://www.javabeginner.com/uncategorized/java-serialization
Suggested read:
You might want to do it with json.
Convert your bean(*) to json an then back.
this proccess is known as serialisation and deserialisation
read about it here
(*) Dont use just members as the data source, build getters and setters for each member in your object.
As stated by others, your question is ambiguous at best. The problem is, you want to represent the object as a string, and then be able to construct the object again from that string.
However, note that while many object types in Java have string representations, this does not guarantee that an object can be constructed from its string representation.
To quote this source,
Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
So, you see, what you want might not be possible. But it is possible to save your object's state to a byte sequence, and then reconstruct it from that byte sequence.
I am storing a class object into a string using toString() method. Now, I want to convert the string into that class object.
First, if I'm understanding your question, you want to store your object into a String and then later to be able to read it again and re-create the Object.
Personally, when I need to do that I use ObjectOutputStream. However, there is a mandatory condition. The object you want to convert to a String and then back to an Object must be a Serializable object, and also all its attributes.
Let's Consider ReadWriteObject
, the object to manipulate and ReadWriteTest
the manipulator.
Here is how I would do it:
public class ReadWriteObject implements Serializable {
/** Serial Version UID */
private static final long serialVersionUID = 8008750006656191706L;
private int age;
private String firstName;
private String lastName;
/**
* @param age
* @param firstName
* @param lastName
*/
public ReadWriteObject(int age, String firstName, String lastName) {
super();
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ReadWriteObject [age=" + age + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
public class ReadWriteTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Create Object to write and then to read
// This object must be Serializable, and all its subobjects as well
ReadWriteObject inputObject = new ReadWriteObject(18, "John", "Doe");
// Read Write Object test
// Write Object into a Byte Array
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(inputObject);
byte[] rawData = baos.toByteArray();
String rawString = new String(rawData);
System.out.println(rawString);
// Read Object from the Byte Array
byte[] byteArrayFromString = rawString.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayFromString);
ObjectInputStream ois = new ObjectInputStream(bais);
Object outputObject = ois.readObject();
System.out.println(outputObject);
}
}
The Standard Output is similar to that (actually, I can't copy/paste it) :
¬í ?sr ?*com.ajoumady.stackoverflow.ReadWriteObjecto$˲é¦LÚ ?I ?ageL ?firstNamet ?Ljava/lang/String;L ?lastNameq ~ ?xp ?t ?John ?Doe
ReadWriteObject [age=18, firstName=John, lastName=Doe]
Class.forName(nameString).newInstance();
You can use the statement :-
Class c = s.getClass();
To get the class instance.
Source: Stackoverflow.com