import java.io.*; public class WriteCharsExample { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("data.txt"); DataOutputStream dos = new DataOutputStream(fos); String str = "Hello, world!"; dos.writeChars(str); dos.close(); fos.close(); System.out.println("Data written to file successfully."); } catch (IOException ex) { System.out.println("Error writing data to file: " + ex.getMessage()); } } }This example writes the string "Hello, world!" to a file named "data.txt" using the writeChars() method of a DataOutputStream object. Package Library: java.lang.Object java.io.OutputStream java.io.FilterOutputStream java.io.DataOutputStream