Java FilterOutputStream write()方法

java.io.FilterOutputStream.write(byte[] b, int off, int len) 用于将len个字节从偏移量写入输出流。

1 语法

public void write(byte[] b, int off, int len)

2 参数

b:要写入流的源缓冲区

off:数据中的起始偏移量

len:要写入的字节数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.FilterOutputStream.write(byte[] b, int off, int len)方法的例子
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Demo {
    public static void main(String[] args) throws Exception {
        OutputStream os = null;
        FilterOutputStream fos = null;
        FileInputStream fis = null;
        byte[] buffer = {65, 66, 67, 68, 69};
        int i = 0;
        char c;

        try {
            // create output streams
            os = new FileOutputStream("d://test.txt");
            fos = new FilterOutputStream(os);

            // writes buffer to the output stream
            fos.write(buffer, 2, 3);

            // forces byte contents to written out to the stream
            fos.flush();

            // create input streams
            fis = new FileInputStream("d://test.txt");

            while((i = fis.read())!=-1) {

                // converts integer to the character
                c = (char)i;

                // prints
                System.out.println("Character read: "+c);
            }

        } catch(IOException e) {
            // if any I/O error occurs
            System.out.print("Close() is invoked prior to write()");
        } finally {
            // releases any system resources associated with the stream
            if(os!=null)
                os.close();
            if(fos!=null)
                fos.close();
        }
    }
}

输出结果为:

Character read: C
Character read: D
Character read: E

热门文章

优秀文章