Java.io.PrintWriter.print(String s) 方法



描述

java.io.PrintWriter.print() 方法用於列印字串。如果引數為 null,則列印字串 "null"。否則,字串的字元將根據平臺的預設字元編碼轉換為位元組,這些位元組將以與 write(int) 方法完全相同的方式寫入。

宣告

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

public void print(String s)

引數

s − 要列印的字串。

返回值

此方法不返回值。

異常

示例

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

package com.tutorialspoint;

import java.io.*;

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

         // print string
         pw.print(s);

         // change the line
         pw.println();

         // print another string
         pw.print("This is an example.");

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

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

Hello world.
This is an example.
java_io_printwriter.htm
廣告
© . All rights reserved.