Java.io.RandomAccessFile.write() 方法



描述

java.io.RandomAccessFile.writeBoolean(boolean v) 方法將布林值寫入檔案作為一個位元組的值。值 true 寫入為 (byte)1;值 false 寫入為 (byte)0。寫入從檔案指標的當前位置開始。

宣告

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

public final void writeBoolean(boolean v)

引數

v − 要寫入的布林值。

返回值

此方法不返回值。

異常

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

示例

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

package com.tutorialspoint;

import java.io.*;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
   
      try {
         boolean b = true;
         
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

         // write a boolean
         raf.writeBoolean(false);

         // set the file pointer at 0 position
         raf.seek(0);

         // print the boolean
         System.out.println("" + raf.readBoolean());

         // write a boolean
         raf.writeBoolean(b);

         // set the file pointer at position 1
         raf.seek(1);

         // print the boolean
         System.out.println("" + raf.readBoolean());
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

假設我們有一個文字檔案 c:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入 -

ABCDE  

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

false
true
java_io_randomaccessfile.htm
廣告