Search code examples
javajavapoet

How to force Javapoet to create UTF-8 Java source code?


Is there a way to write a com.squareup.javapoet.JavaFile to the file system and make sure the file always is encoded using UTF-8?

Currently I am using com.squareup.javapoet.JavaFile#writeTo(java.io.File dir) but this uses the default encoding of the current virtual machine.


Solution

  • Looking at the Github-Page of Javapoet, you can use the Method:

    JavaFile.writeTo(PrintStream)
    

    So you should be able to create a PrintStream using UTF-8 und write the file like this:

    PrintStream stream = new PrintStream("YourTargetFile.java", "UTF-8");
    yourJavaFileObject.writeTo(stream);