Java.io.PrintWriter.write() 方法



描述

java.io.PrintWriter.write() 方法用於寫入字串。

宣告

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

public void write(String s)

引數

s - 要寫入的字串。

返回值

此方法不返回值。

異常

示例

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

package com.tutorialspoint;

import java.io.*;

public class PrintWriterDemo {
   public static void main(String[] args) {
      String s = "Hello";
      
      try {
         // create a new writer
         PrintWriter pw = new PrintWriter(System.out);

         // write strings
         pw.write(s);
         pw.write(" World");

         // flush the writer
         pw.flush();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

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

Hello World
java_io_printwriter.htm
廣告