Java.io.Writer.append() 方法



描述

java.io.Writer.append(CharSequence csq,int start,int end) 方法將指定字元序列的子序列追加到此寫入器。Appendable。

宣告

以下是java.io.Writer.append() 方法的宣告。

public Writer append(CharSequence csq,int start,int end)

引數

  • csq − 要追加的字元序列。如果 csq 為 null,則將四個字元“null”追加到此寫入器。

  • start − 子序列中第一個字元的索引。

  • end − 子序列中最後一個字元之後的字元的索引。

返回值

此方法返回此寫入器。

異常

  • 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
java_io_writer.htm
廣告

© . All rights reserved.