Results 1 to 7 of 7
  1. #1
    Gelembjuk is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default 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?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    sproket is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    You can use the built-in java serialization. Or serialize java objects to XML. Do a google search "Java Serialization" to find out more.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    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.

  5. #5
    Gelembjuk is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    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?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Hmm. And then how do you save the String?

    db

  7. #7
    Gelembjuk is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    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.

    Java 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();
    and sobj contains data that then can be read back to object

    Java Code:
    MyClass b=new MyClass();
          
            ByteArrayInputStream input = new ByteArrayInputStream(sobj.getBytes());
            ObjectInput oi = new ObjectInputStream(input);
            b = (MyClass)oi.readObject();
            oi.close();
    This works as i expected.

Similar Threads

  1. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM
  2. Replies: 4
    Last Post: 03-25-2008, 12:16 PM
  3. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:22 PM
  4. Object from String
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:20 PM
  5. String vs Object
    By Gilgamesh in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 10:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •