try { FileOutputStream fos = new FileOutputStream("output.txt"); fos.write("Hello, world!".getBytes()); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); }
try { FileOutputStream fos = new FileOutputStream("output.txt", true); fos.write("More text!".getBytes()); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); }In this example, we create a new FileOutputStream for a file named "output.txt" using the parameter "true" to indicate that we want to append to the existing file rather than overwrite it. We write the string "More text!" to the file and immediately flush the output stream to ensure that the data is written to the file immediately. Finally, we close the output stream. The FileOutputStream class is included in the java.io package library.