RandomAccessFile類writeBoolean()方法
- writeBoolean() 方法可在
java.io
包。 - writeBoolean() 方法用於將給定的布爾值 (val) 作為 1 個字節寫入文件,就布爾值字節而言,1 表示真,而字節 0 表示假。
- writeBoolean() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- writeBoolean() 方法可能在寫入布爾值時拋出異常。
IOException:此異常在執行輸入/輸出操作時可能會拋出異常。
用法:
public final void writeBoolean(boolean val);
參數:
boolean val
– 表示要寫入的布爾值。
返回值:
這個方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of void writeBoolean(boolean val) method of
// RandomAccessFile
import java.io.*;
class RAFWriteBoolean {
public static void main(String[] args) throws Exception {
// Instantiate a random access file
// object with file name and permissions
RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");
// By using writeBoolean() method is to
// write boolean to the file as 1 byte
ra_f.writeBoolean(true);
// by using seek() method is to
// set the current file indicator
// from where read/write could
// start i.e. we set here 0 so reading
// will be done from 0 till EOF
ra_f.seek(0);
// By using readBoolean() method is to
// read boolean as a 8 bit value
boolean status = ra_f.readBoolean();
System.out.println("ra_f.readBoolean():" + status);
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
輸出
ra_f.readBoolean():true
相關用法
- Java RandomAccessFile writeBytes()用法及代碼示例
- Java RandomAccessFile writeUTF()用法及代碼示例
- Java RandomAccessFile writeFloat()用法及代碼示例
- Java RandomAccessFile writeChar()用法及代碼示例
- Java RandomAccessFile writeDouble()用法及代碼示例
- Java RandomAccessFile writeInt()用法及代碼示例
- Java RandomAccessFile writeLong()用法及代碼示例
- Java RandomAccessFile writeShort()用法及代碼示例
- Java RandomAccessFile writeChars()用法及代碼示例
- Java RandomAccessFile close()用法及代碼示例
- Java RandomAccessFile readByte()用法及代碼示例
- Java RandomAccessFile readChar()用法及代碼示例
- Java RandomAccessFile readUnsignedByte()用法及代碼示例
- Java RandomAccessFile getFD()用法及代碼示例
- Java RandomAccessFile readInt()用法及代碼示例
- Java RandomAccessFile readShort()用法及代碼示例
- Java RandomAccessFile seek()用法及代碼示例
- Java RandomAccessFile skipBytes()用法及代碼示例
- Java RandomAccessFile readDouble()用法及代碼示例
- Java RandomAccessFile getChannel()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java RandomAccessFile writeBoolean() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。