Java.io.Writer.append() 方法



描述

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

宣告

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

public Writer append(CharSequence csq)

引數

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

返回值

此方法返回此寫入器。

異常

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!
java_io_writer.htm
廣告
© . All rights reserved.