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 .
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.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here