Java RandomAccessFile writeBoolean()方法

java.io.RandomAccessFile.writeBoolean(boolean v) 方法写入一个布尔值,该文件为一个字节的值。 true值写入列的值(字节)1; false值是写出来的值(字节)0。写入从文件指针的当前位置。

1 语法

public final void writeBoolean(boolean v)

2 参数

v:要写入一个布尔值。

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.RandomAccessFile.writeBoolean(boolean v)方法的例子
 */
import java.io.*;

public class Demo {
    public static void main(String[] args) {

        try {
            boolean b = true;

            // create a new RandomAccessFile with filename test
            RandomAccessFile raf = new RandomAccessFile("d:/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();
        }
    }
}

假设test.txt文件内容如下:

ABCDE  

输出结果为:

false
true

热门文章

优秀文章