writeUnshared(Object): This method is available in the java.io.ObjectOutputStream class of Java.
Syntax:
void java.io.ObjectOutputStream.writeUnshared(Object obj) throws IOException
This method takes one argument. This method writes an "unshared" object to the ObjectOutputStream.
This method is identical to writeObject, except that it always writes the given object as a new, unique object in the stream (as opposed to a back-reference pointing to a previously serialized instance).
Note:
1. An object is written via writeUnshared is always serialized in the same manner as a newly appearing object regardless of whether or not the object has been written previously.
2. If writeObject is used to write an object that has been previously written with writeUnshared, the previous writeUnshared operation is treated as if it were a write of a separate object.
In other words, ObjectOutputStream will never generate back-references to object data written by calls to writeUnshared.
Parameters: One parameter is required for this method.
obj: object to write to the stream.
Returns: NA
Throws:
1. NotSerializableException - if an object in the graph to be serialized does not implement the Serializable interface.
2. InvalidClassException - if a problem exists with the class of an object to be serialized.
3. IOException - if an I/O error occurs during serialization.
Approach
Java
import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;public class ObjectOutputStreamwriteUnshared {public static void main(String[] args) throws IOException {FileOutputStream fileStream =new FileOutputStream("D:\\hello.txt");ObjectOutputStream objStream =new ObjectOutputStream(fileStream);Object obj = "Hello Java Program";objStream.writeUnshared(obj);System.out.println("Successfully writesthe unshared object to the stream");objStream.close();}}
Output:
Successfully writes the unshared object to the stream
Some more methods of ObjectOutputStream
close(): This method must be called to release any resources associated with the stream.
defaultWriteObject(): This method writes the non-static and non-transient fields of the current class to this stream.
flush(): This method flushes the stream.
putFields(): This method retrieves the object used to buffer persistent fields to be written to the stream.
reset(): The state is reset to be the same as a new ObjectOutputStream.
useProtocolVersion(int): This method specifies the stream protocol version to use when writing the stream.
write(byte[]): This method writes an array of bytes.
write(int): This method writes a byte.
write(byte[], int, int): This method writes a sub array of bytes.
writeBoolean(boolean): This method writes a boolean.
writeByte(int): This method writes an 8-bit byte.
writeBytes(String): This method writes a String as a sequence of bytes.
writeChar(int): This method writes a 16-bit char.
writeChars(String): This method writes a String as a sequence of chars.
writeDouble(double): This method writes a 64-bit double.
writeFields(): This method writes the buffered fields to the stream.
writeFloat(float): This method writes a 32-bit float.
writeInt(int): This method writes a 32-bit int.
writeLong(long): This method writes a 64-bit long.
writeObject(Object): This method writes the specified object to the ObjectOutputStream.
writeShort(int): This method writes a 16-bit short.
writeUTF(String): This method is primitive data are written of this String in modified UTF-8 format.
No comments:
Post a Comment