import java.io.*; public class ByteWriter { public static void main(String[] args) throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("output.bin")); dos.writeByte(0x41); // write the ASCII value of 'A' dos.close(); } }
import java.io.*; public class BinaryWriter { public static void main(String[] args) throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("output.bin")); dos.writeByte(0b00000001); // write a single bit dos.writeByte(0b10000000); // write another single bit dos.close(); } }In this example, we create a new `DataOutputStream` and write two single bits (one set to 1 and the other set to 0) to the output stream. We then close the stream. Both of these examples use the `writeByte()` method from the `java.io.DataOutputStream` class to write binary data to an output stream.