How to append text to file in Java 8 using specified Charset

principal-ideal-domain picture principal-ideal-domain · May 18, 2015 · Viewed 19.7k times · Source

I'm looking for an easy and save solution to append text to a existing file in Java 8 using a specified Charset cs. The solution which I found here deals with the standard Charset which is a no-go in my situation.

Answer

assylias picture assylias · May 18, 2015

One way it to use the overloaded version of Files.write that accepts a Charset:

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;

List<String> lines = ...;
Files.write(log, lines, UTF_8, APPEND, CREATE);