ObjectOutputStream drain()方法
java.io.ObjectOutputStream.drain() 此方法用于清除ObjectOutputStream中所有缓冲的数据。与flush()方法类似,但不会将刷新传播到基础流。
1 语法
protected void drain()
2 参数
无
3 返回值
无
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.io.ObjectOutputStream.drain()方法的例子
*/
import java.io.*;
public class Demo extends ObjectOutputStream{
public Demo(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");
Demo oout = new Demo(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();
}
}
}
输出结果为:
Number = 15
Number = 20
Number = 25
热门文章
优秀文章