void write(byte[] b, int off, int len)
描述 (Description)
java.io.FileOutputStream.write(byte[] b, int off, int len)方法将从offset off开始的指定字节数组中的len个字节写入此文件输出流。
声明 (Declaration)
以下是java.io.FileOutputStream.write(byte[] b, int off, int len)方法的声明 -
public void write(byte[] b, int off, int len)
参数 (Parameters)
b - 源缓冲区。
off - 数据中的起始偏移量。
len - 要写入的字节数。
返回值 (Return Value)
此方法不返回任何值。
异常 (Exception)
IOException - 如果发生任何I/O错误。
例子 (Example)
以下示例显示了java.io.FileOutputStream.write(byte [] b,int off,int len)方法的用法。
package com.iowiki;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = null;
FileInputStream fis = null;
byte[] b = {65,66,67,68,69};
int i = 0;
char c;
try {
// create new file output stream
fos = new FileOutputStream("C://test.txt");
// writes byte to the output stream
fos.write(b, 2, 3);
// flushes the content to the underlying stream
fos.flush();
// create new file input stream
fis = new FileInputStream("C://test.txt");
// read till the end of the file
while((i = fis.read())!=-1) {
// convert integer to character
c = (char)i;
// prints
System.out.print(c);
}
} catch(Exception ex) {
// if an error occurs
ex.printStackTrace();
} finally {
// closes and releases system resources from stream
if(fos!=null)
fos.close();
if(fis!=null)
fis.close();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
CDE