Java.io.Writer.append() 方法
描述
java.io.Writer.append(CharSequence csq,int start,int end) 方法将指定字符序列的子序列附加到此编写器。 可追加。
声明
以下是 java.io.Writer.append() 方法的声明。
public Writer append(CharSequence csq,int start,int end)
参数
csq − 要追加的字符序列。 如果 csq 为 null,则将四个字符"null"附加到此 writer。
start − 子序列中第一个字符的索引。
end − 子序列中最后一个字符之后的字符的索引。
返回值
此方法返回此 writer。
异常
IndexOutOfBoundsException − 如果 start 或 end 为负数,则 start 大于 end,或者 end 大于 csq.length()。
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 subsequence and change line
writer.append("This is an example\n", 11, 19);
// flush the writer
writer.flush();
// append a new subsequence
writer.append(csq, 0, 5);
// flush the writer to see the result
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
example Hello

