
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectOutputStream drain() method
Description
The Java ObjectOutputStream drain() method drains any buffered data in ObjectOutputStream. Similar to flush but does not propagate the flush to the underlying stream.
Declaration
Following is the declaration for java.io.ObjectOutputStream.drain() method.
protected void drain()
Parameters
NA
Return Value
This method does not return a value.
Exception
IOException − If I/O errors occur while writing to the underlying OutputStream.
Example - Usage of ObjectOutputStream drain() method
The following example shows the usage of ObjectOutputStream drain() method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; public class ObjectOutputStreamDemo extends ObjectOutputStream{ public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) { int i = 319874; try { // create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStreamDemo oout = new ObjectOutputStreamDemo(out); // write something in the file oout.writeInt(i); oout.writeInt(1653984); // drain the stream oout.drain(); // close the stream oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print an int System.out.println("" + ois.readInt()); // read and print an int System.out.println("" + ois.readInt()); } catch (Exception ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
319874 1653984
Example - Using drain() method before writing a second object
The following example shows the usage of ObjectOutputStream drain() method. We're writing multiple objects to a file then using drain() method between them to ensure any buffered data is cleared before writing the next one.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; public class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) { String filename = "drain_multiple_objects.ser"; try (FileOutputStream fos = new FileOutputStream(filename); ObjectOutputStreamDemo oos = new ObjectOutputStreamDemo(fos)) { // Write first object oos.writeObject("First object"); System.out.println("First object written."); // Drain buffered data (but do not flush underlying stream) oos.drain(); System.out.println("Buffer drained."); // Write second object oos.writeObject("Second object"); System.out.println("Second object written."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
First object written. Buffer drained. Second object written.
Explanation
We subclass ObjectOutputStream to access the protected drain() method.
We write "First object", then call drain() - this flushes the internal buffer only, not the file stream.
Then we write "Second object".
Example - Simple example of drain() method
The following example shows the usage of ObjectOutputStream drain() method. We'll write extra info to the stream (e.g. version info).
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; public class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) { int number = 12345; try { // Create a new file with an ObjectOutputStream FileOutputStream fileOut = new FileOutputStream("example.ser"); ObjectOutputStreamDemo objectOut = new ObjectOutputStreamDemo(fileOut); // Write an integer to the file objectOut.writeInt(number); // Drain the stream objectOut.drain(); System.out.println(" drain() method called."); // Close the stream objectOut.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
drain() method called.
Explanation
Since the drain() method is protected, it can only be accessed within subclasses or within the same package. Therefore, to use drain(), you would typically need to create a subclass of ObjectOutputStream, as shown in the example above.
The drain() method is particularly useful when you want to ensure that all buffered data within the ObjectOutputStream is written out without affecting the underlying stream.