當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java 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" 附加到此 writer。

  • 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.append() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。