void write(char[] cbuf, int off, int len)
描述 (Description)
java.io.OutputStreamWriter.write(char[] cbuf, int off, int len)方法写入字符数组的一部分。
声明 (Declaration)
以下是java.io.OutputStreamWriter.write()方法的声明。
public void write(char[] cbuf, int off, int len)
参数 (Parameters)
cbuf - 字符缓冲区。
off - 从中开始写字符的偏移量。
len - 要写入的字符数。
返回值 (Return Value)
此方法不返回值。
异常 (Exception)
IOException - 如果发生I/O错误。
例子 (Example)
以下示例显示了java.io.OutputStreamWriter.write()方法的用法。
package com.iowiki;
import java.io.*;
public class OutputStreamWriterDemo {
public static void main(String[] args) {
char[] arr = {'H', 'e', 'l', 'l', 'o'};
try {
// create a new OutputStreamWriter
OutputStream os = new FileOutputStream("test.txt");
OutputStreamWriter writer = new OutputStreamWriter(os);
// create a new FileInputStream to read what we write
FileInputStream in = new FileInputStream("test.txt");
// write something in the file
writer.write(arr, 0, 3);
// flush the stream
writer.flush();
// read what we write
for (int i = 0; i < 3; i++) {
System.out.print("" + (char) in.read());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Hel