I want to write into a file using UTF-16 so I use PrintWriter(file,"UTF-16"), but then it deletes everything in the file, I could use FileWriter(file,true), but then it wouldn't be in UTF-16, and there apparently isn't a constructor for PrintWriter like PrintWriter(Writer,Charset,boolean append);
What should I do?
Use OutputStreamWriter
with a UTF-16 charset, wrapping a FileOutputStream
opened with append=true. Alternatives, use Files.newBufferedWriter
:
try (Writer writer = Files.newBufferedWriter(
Paths.of("filename.txt"),
StandardCharsets.UTF_16,
StandardOpenOption.APPEND)) {
...
}
See more on this question at Stackoverflow