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


Java ObjectOutputStream writeBoolean()用法及代碼示例


ObjectOutputStream類writeBoolean()方法

  • writeBoolean() 方法可在java.io包。
  • writeBoolean() 方法用於寫入給定的布爾值。
  • writeBoolean() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • writeBoolean() 方法可能在寫入布爾值時拋出異常。
    IOException:在寫入輸出流時出現任何輸入/輸出錯誤時,可能會拋出此異常。

用法:

    public void writeBoolean(boolean value);

參數:

  • boolean value– 表示要寫入的布爾值。

返回值:

該方法的返回類型是void,它什麽都不返回。

例:

// Java program to demonstrate the example 
// of void writeBoolean(boolean value) method of //ObjectOutputStream

import java.io.*;

public class WriteBooleanOfOOS {
 public static void main(String[] args) throws Exception {
  // Instantiates ObjectOutputStream , ObjectInputStream 
  // FileInputStream and FileOutputStream
  FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
  ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
  FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
  ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

  // By using writeBoolean() method is to
  // write the boolean to the stream		
  obj_out_stm.writeBoolean(true);
  obj_out_stm.writeBoolean(false);

  obj_out_stm.flush();


  // By using readBoolean() method is to 
  // read the boolean
  boolean b1 = (boolean) obj_in_stm.readBoolean();
  boolean b2 = (boolean) obj_in_stm.readBoolean();

  System.out.println("obj_in_stm.readBoolean():" + b1);
  System.out.println("obj_in_stm.readBoolean():" + b2);


  // By using close() method is to 
  // close all the streams 
  System.out.println("Stream Shutdown... ");

  file_in_stm.close();
  file_out_stm.close();
  obj_in_stm.close();
  obj_out_stm.close();
 }
}

輸出

obj_in_stm.readBoolean():true
obj_in_stm.readBoolean():false
Stream Shutdown... 


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ObjectOutputStream writeBoolean() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。