How to make the PrintWriter to write UTF-8?

yeah its me picture yeah its me · Jan 13, 2014 · Viewed 58.1k times · Source

How to make the PrintWriter to write UTF-8?

pstream = new PrintWriter(csocket.getOutputStream(), true);
String res = "some string";
pstream.println(res); // here I want to output string as UTF-8

Answer

Njol picture Njol · Jan 13, 2014

Use an OutputStreamWriter:

pstream = new PrintWriter(new OutputStreamWriter(
    csocket.getOutputStream(), StandardCharsets.UTF_8), true)