import java.io.*; public class WriteDataExample { public static void main(String[] args) { // Step 1: Declare OutputStream variables OutputStream outputStream = null; BufferedOutputStream bufferedOutput = null; try { // Step 2: Create FileOutputStream to write to a file outputStream = new FileOutputStream("data.txt"); // Step 3: Wrap FileOutputStream with BufferedOutputStream bufferedOutput = new BufferedOutputStream(outputStream); // Step 4: Write data as bytes String message = "Hello PVPSIT, this is buffered output!"; byte[] data = message.getBytes(); // Convert string to byte array bufferedOutput.write(data); // Write bytes to buffer // Step 5: Flush the buffer to ensure data is written to file bufferedOutput.flush(); System.out.println("Data written successfully."); } catch (IOException e) { System.out.println("Error writing to file: " + e.getMessage()); } finally { // Step 6: Close streams in reverse order of opening try { if (bufferedOutput != null) bufferedOutput.close(); if (outputStream != null) outputStream.close(); } catch (IOException e) { System.out.println("Error closing streams: " + e.getMessage()); } } } }