Java.io.Writer.write() 方法



描述

java.io.Writer.write(String str) 方法用於寫入字串。

宣告

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

public void write(String str)

引數

str − 要寫入的字串。

返回值

此方法不返回值。

異常

IOException − 如果發生 I/O 錯誤。

示例

以下示例演示了 java.io.Writer.write() 方法的使用。

package com.tutorialspoint;

import java.io.*;

public class WriterDemo {
   public static void main(String[] args) {
      String str = "Hello world!";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write a string
         writer.write(str);

         // flush the writer
         writer.flush();

         // change line and write another string
         writer.write("\nThis is an example");

         // flush the stream again
         writer.flush();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行以上程式,這將產生以下結果:

Hello world!
This is an example
java_io_writer.htm
廣告

© . All rights reserved.