
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectOutputStream writeBoolean(boolean val) method
Description
The Java ObjectOutputStream writeBoolean(boolean val) method writes a boolean.
Writes a single boolean value to the output stream.
true is written as byte 1, false as byte 0.
This method is inherited from DataOutput, implemented by ObjectOutputStream.
This is not object serialization. It writes raw binary data.
Declaration
Following is the declaration for java.io.ObjectOutputStream.writeBoolean(boolean val) method.
public void writeBoolean(boolean val)
Parameters
val − The boolean to be written.
Return Value
This method does not return a value.
Exception
IOException − If I/O errors occur while writing to the underlying stream.
Example - Usage of ObjectOutputStream writeBoolean(boolean val) method
The following example shows the usage of ObjectOutputStream writeBoolean(boolean val) method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; 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(); } } }
Output
Let us compile and run the above program, this will produce the following result −
true false
Example - Write a single boolean value and read it back
The following example shows the usage of ObjectOutputStream writeBoolean(boolean val) method. We're writing true to a file and verify it by reading it back.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectOutputStreamDemo { public static void main(String[] args) { String filename = "bool1.bin"; // Write a boolean value try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { oos.writeBoolean(true); // writes byte 1 System.out.println("Wrote boolean: true"); } catch (IOException e) { e.printStackTrace(); } // Read it back try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { boolean value = ois.readBoolean(); System.out.println("Read boolean: " + value); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Wrote boolean: true Read boolean: true
Example - Write a list of booleans to a file and read them back
The following example shows the usage of ObjectOutputStream writeBoolean(boolean val) method. We're writing multiple boolean values using writeBoolean() method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectOutputStreamDemo { public static void main(String[] args) { String filename = "bool_list.bin"; boolean[] flags = {true, false, true, true, false}; // Write the list of booleans try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { for (boolean b : flags) { oos.writeBoolean(b); } System.out.println("Boolean array written."); } catch (IOException e) { e.printStackTrace(); } // Read them back try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { System.out.println("Reading booleans:"); for (int i = 0; i < flags.length; i++) { System.out.println(" - " + ois.readBoolean()); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Boolean array written. Reading booleans: - true - false - true - true - false