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


Java Java.io.PrintWriter.print()用法及代碼示例


描述

這個java.io.PrintWriter.print()方法打印一個布爾值。 String.valueOf(boolean) 生成的字符串根據平台默認的字符編碼轉換為字節,這些字節的寫入方式與 write(int) 方法完全一致。

聲明

以下是聲明java.io.PrintWriter.print()方法。

public void print(boolean b)

參數

b─ 要打印的布爾值。

返回值

此方法不返回值。

異常

NA

示例

下麵的例子展示了使用java.io.PrintWriter.print()方法。

package com.tutorialspoint;

import java.io.*;

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

         // print a boolean
         pw.print(true);

         // change the line
         pw.println();

         // print another boolean
         pw.print(bool);

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

讓我們編譯並運行上麵的程序,這將產生以下結果——

true
false

相關用法


注:本文由純淨天空篩選整理自 Java.io.PrintWriter.print() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。