PipedOutputStream类flush()方法flush()方法在java.io包中可用。
flush()方法用于刷新此PipedOutputStream并强制从任何缓冲输出中写出字节。
flush()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
flush()方法在刷新流时可能会引发异常。
IOException:遇到任何输入/输出错误时,可能引发此异常。
语法:public void flush();
参数:它不接受任何参数。
返回值:
该方法的返回类型为void,不返回任何内容。
示例//Java程序演示示例
//flush()PipedOutputStream的void方法的说明
import java.io.*;
public class FlushOfPOS {
public static void main(String[] args) throws Exception {
int val = 65;
try {
//实例化PipedInputStream和
//PipedOutputStream-
PipedInputStream pipe_in = new PipedInputStream();
PipedOutputStream pipe_out = new PipedOutputStream();
//通过使用connect()方法是
//将此pipe_out连接到给定的pipe_in-
pipe_out.connect(pipe_in);
for (int i = 0; i
//通过使用write()方法是
//将val写入流pipe_out-
pipe_out.write(val);
val++;
}
//通过使用flush()方法是
//冲洗流
pipe_out.flush();
System.out.println("Stream Flushed!!!");
for (int i = 1; i
//通过使用read()方法是读取
//整数并转换为int-
char ch = (char) pipe_in.read();
System.out.println("pipe_in.read(): " + ch);
}
//通过使用close()方法是关闭
//流
pipe_in.close();
pipe_out.close();
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
输出结果Stream Flushed!!!
pipe_in.read(): A
pipe_in.read(): B
pipe_in.read(): C