Java.io.Writer.append() 方法
描述
java.io.Writer.append(CharSequence csq) 方法将指定的字符序列附加到这个 writer。
声明
以下是 java.io.Writer.append() 方法的声明。
public Writer append(CharSequence csq)
参数
csq − 要追加的字符序列。 如果 csq 为 null,则将四个字符"null"附加到此 writer。
返回值
此方法返回此 writer。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.Writer.append() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class WriterDemo {
public static void main(String[] args) {
CharSequence csq = "Hello World!";
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// append a sequence and change line
writer.append("This is an example\n");
// flush the writer
writer.flush();
// append a new sequence
writer.append(csq);
// flush the writer to see the result
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
This is an example Hello World!

