Java.io.Writer.write() 方法
描述
java.io.Writer.write(char[] cbuf,int off,int len) 方法写入字符数组的一部分。
声明
以下是 java.io.Writer.write() 方法的声明。
public abstract void write(char[] cbuf,int off,int len)
参数
cbuf − 要写入的字符数组。
off − 开始写入字符的偏移量。
len − 要写入的字符数。
返回值
此方法不返回值。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.Writer.write() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class WriterDemo {
public static void main(String[] args) {
char[] c = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// write a portion of a char array
writer.write(c, 0, 5);
// flush the writer
writer.flush();
// write another portion of a char array
writer.write(c, 5, 5);
// flush the stream again
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
helloworld

