Java.io.Writer.write() 方法
描述
java.io.Writer.write(String str) 方法写入一个字符串。
声明
以下是 java.io.Writer.write() 方法的声明。
public void write(String str)
参数
str − 要写入的字符串。
返回值
此方法不返回值。
异常
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
writer.write(str);
// flush the writer
writer.flush();
// change line and write another string
writer.write("\nThis is an example");
// flush the stream again
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Hello world! This is an example

