Search code examples
javafileloggingjava-8filewriter

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


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.


Solution

  • 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);