0

Can i write oblects in java into file and retrieve the same?? In c++ we ise wrie function.. can i realize this in java?

In C++ we may use

fin.write((char*)&object,sizeof(classname));

how can i realize this in java.?

1 Answer 1

2

You can use an ObjectOutputStream to serialize objects to a file (or any other output stream) and ObjectInputStream to read them back in. Objects need to implement the Serializable interface to be useful with these classes. Not all objects can be serialized. (For example, it wouldn't make sense or be very useful to serialize a socket.)

Sign up to request clarification or add additional context in comments.

1 Comment

+1: One difference is that Java will serialize references correctly. In C it will copy just the value of the pointer at the time, which isn't useful when deserializing. In Java it will copy the contents of the objects referenced and deserialize it appropriately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.