Java.io.ObjectOutputStream.writeBoolean()方法实例
java.io.ObjectOutputStream.writeBoolean(boolean val) 方法写入一个布尔值。
声明
以下是java.io.ObjectOutputStream.writeBoolean()方法声明
public void writeBoolean(boolean val)
参数
-
val -- 将要写入的布尔值
返回值
此方法没有返回值。
异常
-
IOException -- 如果在写入底层流发生I / O错误
例子
下面的示例演示java.io.ObjectOutputStream.writeBoolean()方法的用法。
package com.yiibai; import java.io.*; public class ObjectOutputStreamDemo { public static void main(String[] args) { boolean b = true; try { // create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeBoolean(b); oout.writeBoolean(false); // close the stream oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print what we wrote before System.out.println("" + ois.readBoolean()); System.out.println("" + ois.readBoolean()); } catch (Exception ex) { ex.printStackTrace(); } } }
让我们编译和运行上面的程序,这将产生以下结果:
true false