import java.io.*; public class DataOutputStreamExample { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("output.txt"); DataOutputStream dos = new DataOutputStream(fos); dos.writeFloat(3.14f); dos.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class DataOutputStreamExample { public static void main(String[] args) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeFloat(1.23f); byte[] bytes = baos.toByteArray(); for (byte b : bytes) { System.out.printf("%02x ", b); } dos.close(); baos.close(); } catch (IOException e) { e.printStackTrace(); } } }In this example, we create a ByteArrayOutputStream object that is not connected to any file or device. Then, we use the writeFloat method to write the float value 1.23 to the output stream. We retrieve the byte array containing the data from the ByteArrayOutputStream and print each byte in hexadecimal format. Finally, we close the streams. The DataOutputStream class is part of the java.io package library.