String data = "Example String"; try { FileOutputStream outputStream = new FileOutputStream("example.txt"); IOUtils.write(data, outputStream); outputStream.close(); } catch (IOException e) { // Handle exception }
byte[] data = {1, 2, 3}; try { FileOutputStream outputStream = new FileOutputStream("example.bin"); IOUtils.write(data, outputStream); outputStream.close(); } catch (IOException e) { // Handle exception }In both examples, the IOUtils class is used to write data to an output stream, which is then closed to ensure proper resource management. The org.apache.commons.io package contains the IOUtils class and other IO-related utility classes.