How do I write a text file in JDK 7?

Share:

In JDK 7 we can write all lines from a List of String into a file using the Files.write() method. We need to provide the Path of the file we want to write to, the List of strings and the charsets. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform’s line separator.

Let’s see the code snippet below:

This code snippet will create a file called data.txt under the resources folder. Please make sure that this folder is exist before you tried to run the code.