Results 1 to 7 of 7
Thread: Object to String.
- 10-28-2008, 09:14 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 9
- Rep Power
- 0
Object to String.
Hello.
I want somehow to save object to string and later restore it.
I have class MyClass. I want to create object, work with it and then save object properties to string. Later to start program and init object from that string.
I see that there is toString() method. But i am not sure it do that i need.
The class i code will be extended and i don't know now what properties will final class have.
How to save the object to string?
- 10-28-2008, 09:24 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you read that what toString() method is doing?
Can you show what you have done so far.
- 10-28-2008, 12:32 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 7
- Rep Power
- 0
You can use the built-in java serialization. Or serialize java objects to XML. Do a google search "Java Serialization" to find out more.
- 10-28-2008, 02:11 PM #4
You want to save the contents of an instance of your class in a disk file so that you can later read that file and recreate the object.
One way is to use serialization, another is to convert all the class's variables contents to a String and then write it a file. To get the object back, have a constructor in that class that will take the String that was written to the file and create an instance of the class.
- 10-29-2008, 09:49 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 9
- Rep Power
- 0
Thanks.
Yes. I think something like serialization.
I have found some examples but there object is saved to file. I don't want to save object to file. I want to save it to string.
Can you write example where object it serialised and saved to string variable not in file?
- 10-29-2008, 10:12 PM #6
Hmm. And then how do you save the String?
db
- 10-29-2008, 10:19 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 9
- Rep Power
- 0
I have found the way to do this.
That is not readable string. But i must convert that bytes to string becouse it is the only way to transfer the object to externall app and the get it back.
and sobj contains data that then can be read back to objectJava Code:String sobj=""; MyClass a=new MyClass(); a.setData("text"); try{ ByteArrayOutputStream fos = new ByteArrayOutputStream(); ObjectOutputStream outStream = new ObjectOutputStream( fos ); outStream.writeObject( a ); outStream.flush(); sobj=fos.toString();
This works as i expected.Java Code:MyClass b=new MyClass(); ByteArrayInputStream input = new ByteArrayInputStream(sobj.getBytes()); ObjectInput oi = new ObjectInputStream(input); b = (MyClass)oi.readObject(); oi.close();
Similar Threads
-
Converting object to string
By Preethi in forum New To JavaReplies: 4Last Post: 06-14-2008, 03:29 AM -
Method to write if the string object can be converted to Integer or not.
By Abhishek in forum New To JavaReplies: 4Last Post: 03-25-2008, 12:16 PM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:22 PM -
Object from String
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:20 PM -
String vs Object
By Gilgamesh in forum New To JavaReplies: 1Last Post: 11-28-2007, 10:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks