import java.io.*; public class Example { public static void main(String[] args) { try { DataOutputStream dos = new DataOutputStream( new FileOutputStream("data.txt")); dos.writeDouble(3.14159); // write a double value dos.writeInt(42); // write an integer value dos.close(); // close the stream } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class Example { public static void main(String[] args) { try { OutputStream os = new FileOutputStream("data.txt"); DataOutputStream dos = new DataOutputStream(os); dos.writeChars("Hello, World!"); // write a string value dos.writeBoolean(true); // write a boolean value dos.close(); // close the stream } catch (IOException e) { e.printStackTrace(); } } }This example shows how to create a DataOutputStream object and write a string and boolean value to an output stream. The close() method is called to release any resources associated with the stream. The DataOutputStream class is part of the java.io package library in Java.