How to write file using Files.newBufferedWriter?

To open a file for writing in JDK 7 you can use the Files.newBufferedWriter() method. This method takes three arguments. We need to pass the Path, the Charset and a vargars of OpenOption.

For example, in the snippet below we pass the path of our log file, we use the StandardCharsets.UTF_8 charset and we use the StandardOpenOption.WRITE to open a file for writting. If you want to open a file and append its contents instead of rewriting it you can use the StandardOpenOption.APPEND.

  • Aaron Kelly

    thank you!