Java.io.Writer.write() 方法
描述
java.io.Writer.write(String str,int off,int len) 方法写入字符串的一部分。
声明
以下是 java.io.Writer.write() 方法的声明。
public void write(String str,int off,int len)
参数
str − 要写入的字符串。
off − 开始写入字符的偏移量。
len − 要写入的字符数。
返回值
此方法不返回值。
异常
IndexOutOfBoundsException − 如果 off 是负数,或者 len 是负数,或者 off+len 是负数或大于给定字符串的长度。
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.Writer.write() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class WriterDemo {
public static void main(String[] args) {
String str = "Hello world!";
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// write a string portion
writer.write(str, 0, 5);
// flush the writer
writer.flush();
// write another string portion
writer.write(str, 5, 6);
// flush the stream again
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Hello world

