Write boolean to a file using DataOutputStream
- /*
- Write boolean to a file using DataOutputStream
- This Java example shows how to write a Java boolean primitive value to a file using
- writeBoolean method of Java DataOutputStream class.
- */
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class WriteBooleanToFile {
- public static void main(String[] args) {
- String strFilePath = "C://FileIO//WriteBoolean.txt";
- try
- {
- //create FileOutputStream object
- FileOutputStream fos = new FileOutputStream(strFilePath);
- /*
- * To create DataOutputStream object from FileOutputStream use,
- * DataOutputStream(OutputStream os) constructor.
- *
- */
- DataOutputStream dos = new DataOutputStream(fos);
- boolean b = false;
- /*
- * To write a boolean value to a file, use
- * void writeBoolean(boolean b) method of Java DataOutputStream class.
- *
- * This method writes specified boolean to output stream as a 1 byte (true
- * value is written out as 1 whereas false as 0)
- */
- dos.writeBoolean(b);
- /*
- * To close DataOutputStream use,
- * void close() method.
- *
- */
- dos.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException : " + e);
- }
- }
- }



