Writing to a File with Files.write in Java


Use Files.write for a concise way to write to files, automatically handling resource management.

Source Code

List lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("output.txt");
try {
    Files.write(file, lines, Charset.forName("UTF-8"));
} catch (IOException e) {
    e.printStackTrace();
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments