aspose file tools*
The moose likes Java in General and the fly likes how to do this with serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to do this with serialization " Watch "how to do this with serialization " New topic
Author

how to do this with serialization

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 682

can i do this with serialization
suppose i have a string in my class called

  • 1 now first i have used ObjectOutputStream to write the whole object in a file
  • 2 then i am reading the object say like




  • 3 so i guess readobject is reading the object the object inturn has a string called "s"
    so my question is can i print that string s after deleting the string s from my sample class


  • why i m asking this question because we have stored the whole object in a file so when we are reading that file we should get all the instance variable which are stored in that Object if yes than how one can print those instance variable on the console even though they didnt exit after being written to a file .


    The Only way to learn is ...........do!
    Visit my blog http://inaved-momin.blogspot.com/
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7081
        
      16

    naved momin wrote:...so when we are reading that file we should get all the instance variable which are stored in that Object if yes than how one can print those instance variable on the console even though they didnt exit after being written to a file .

    You're mixing several inaccuracies, so it's a bit difficult to know where to start:

    1. Your String would be read with
    String s = (String) objectInputStream.readObject();
    not what you had.

    2. Once you read it back in, the String will exist as a new String, regardless of whether you deleted the original or not; so the answer to your question 3 is yes, you can print it.

    3. An object would only not exist if you deleted it, whether you wrote it to a file first or not. The two actions are completely unrelated.

    4. Printing out instance variables depends entirely on whether the object allows you access to them or not. In the case of String, it doesn't. For some other object that you define, maybe it does. But again, this is totally unrelated to Serialization.

    5. What instance fields get initialized depends entirely on how you serialize. If you use the default methodology, all instance fields will get initialized unless they have the qualifier transient.

    I suggest you read this, or look at the tutorials on DataStreams and ObjectStreams.

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Artlicles by Winston can be found here
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to do this with serialization
     
    Similar Threads
    Please explain me answer. (static)
    transient variable ? is there any other way
    serialization: object casting doubt
    SCJP (now OCPJP) 1.6 Exam No. 1Z0-851, how to prepare?
    How to print java Map name/value pair through JSON object in JS