Java BufferedOutputStream flush()方法

java.io.BufferedInputStream.flush() 刷新缓冲的输出流。

1 语法

public void flush()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.BufferedInputStream.flush()方法的例子
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) throws Exception {
        FileInputStream is = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bos = null;

        try {
            // open input stream test.txt for reading purpose.
            is = new FileInputStream("d:/test.txt");

            // input stream is converted to buffered input stream
            bis = new BufferedInputStream(is);

            // creates a new byte array output stream
            baos = new ByteArrayOutputStream();

            // creates a new buffered output stream to write 'baos'
            bos = new BufferedOutputStream(baos);

            int value;

            // the file is read to the end
            while ((value = bis.read()) != -1) {
                bos.write(value);
            }

            // invokes flush to force bytes to be written out to baos
            bos.flush();

            // every byte read from baos
            for (byte b: baos.toByteArray()) {

                // converts byte to character
                char c = (char)b;
                System.out.print(c);
            }
        } catch(IOException e) {
            // if any IOException occurs
            e.printStackTrace();
        } finally {
            // releases any system resources associated with the stream
            if(is!=null)
                is.close();
            if(bis!=null)
                bis.close();
            if(baos!=null)
                baos.close();
            if(bos!=null)
                bos.close();
        }
    }
}

输出结果为:

ABCDE

热门文章

优秀文章