What are the methods of ObjectOutputStream?
Method Summary
Modifier and Type | Method and Description |
---|---|
void | writeFloat(float val) Writes a 32 bit float. |
void | writeInt(int val) Writes a 32 bit int. |
void | writeLong(long val) Writes a 64 bit long. |
void | writeObject(Object obj) Write the specified object to the ObjectOutputStream. |
How do you use ObjectOutputStream in Java?
The ObjectOutputStream class of the java.io package can be used to write objects that can be read by ObjectInputStream . It extends the OutputStream abstract class….Other Methods Of ObjectOutputStream.
Methods | Descriptions |
---|---|
drain() | puts all the buffered data in the output stream |
close() | closes the output stream |
How do you write an object to OutputStream?
4. Methods in OutputStream
- 4.1. write(int b) We can use this method to write one specific byte to the OutputStream.
- 4.2. write(byte[] b, int off, int length) This overloaded version of the write() method is there to write a sub-sequence of the byte array to the OutputStream.
- 4.3. write(byte[] b)
How do you read ObjectInputStream?
Example 1
- import java.io.*;
- public class ObjectInputStreamreadExample3 {
- public static void main(String[] args) {
- String str = “Hello”;
- byte[] barray = {‘j’, ‘a’, ‘v’, ‘a’, ‘T’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’};
- try {
- // create a new file with an ObjectOutputStream.
- FileOutputStream outstream = new FileOutputStream(“file1.txt”);
What is Serialisation and Deserialization in Java?
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. So, the object serialized on one platform can be deserialized on a different platform.
How do you write objects to a random access file?
A random access file behaves like a large array of bytes. There is a cursor implied to the array called file pointer, by moving the cursor we do the read write operations….Method.
Modifier and Type | Method | Method |
---|---|---|
FileChannel | getChannel() | It returns the unique FileChannel object associated with this file. |
Which of these is method of ObjectOutputStream class used to write the object to output stream as required?
writeObject()
Which of these is method of ObjectOutput interface used to write the object to input or output stream as required? Explanation: writeObject() is used to write an object into invoking stream, it can be input stream or output stream. 7.
Which method writes the specified object to the ObjectOutputStream?
writeObject(Object obj) method writes the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written.
What are the requirements for a class that you want to serialize with ObjectOutputStream?
Classes that are eligible for serialization need to implement a special marker interface, Serializable. Both ObjectInputStream and ObjectOutputStream are high level classes that extend java. io. InputStream and java.
What is ObjectInputStream and ObjectOutputStream in Java?
An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The ObjectOutputStream is used to write the Java objects, and the ObjectInputStream is used to read the objects again.