try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { writer.write("This is some text that will be written to a file."); writer.close(); } catch (IOException ex) { // handle exception }
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))) { writer.write("This is some text that will be written to the console."); writer.close(); } catch (IOException ex) { // handle exception }In this example, we create a BufferedWriter that wraps an OutputStreamWriter object for writing text to the console. We write some text to the buffer and then close the writer. Both examples use the java.io package library.