java.io.RandomAccessFile.writeBoolean(boolean v) 方法写入一个布尔值,该文件为一个字节的值。 true值写入列的值(字节)1; false值是写出来的值(字节)0。写入从文件指针的当前位置。
声明
以下是java.io.RandomAccessFile.write()方法的声明
public final void writeBoolean(boolean v)
参数
-
v -- 要写入一个布尔值。
返回值
此方法不返回任何值。
异常
-
IOException -- 如果发生I/O错误。
例子
下面的示例演示java.io.RandomAccessFile.write()方法的用法。
package com.yiibai; 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