Java.io.CharArrayWriter.append() 方法
描述
java.io.CharArrayWriter.append(CharSequence csq, int start, int end) 方法将指定字符序列的一部分附加到此编写器。
声明
以下是 java.io.CharArrayWriter.append(CharSequence csq, int start, int end) 方法的声明 −
public CharArrayWriter append(CharSequence csq, int start, int end)
参数
csq − 要附加的字符序列。 如果字符序列为 null,则 writer 附加 4 个字符 'null'。
start − 该部分的第一个字符的索引
end − 子序列末尾字符的索引
返回值
这个 CharArrayWriter
异常
IndexOutOfBoundsException − 如果 end 大于序列的长度,则 start 大于 end,或者 start 或 end 为负数。
示例
下面的例子展示了 java.io.CharArrayWriter.append(CharSequence csq, int start, int end) 方法的使用。
package com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
CharArrayWriter chw = null;
try {
// create character array writer
chw = new CharArrayWriter();
// declare character sequence
CharSequence csq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// append character sequence to the writer
chw.append(csq, 2, 5);
// prints out the character sequences
System.out.println(chw.toString());
} catch(Exception e) {
// for any error
e.printStackTrace();
} finally {
// releases all system resources from writer
if(chw!=null)
chw.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
CDE

