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
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
you can only write to file with any charset, otherwise platform default charset used see doc
Use an
OutputStreamWriter
:Don't user PrintWriter. If you need UTF-8 encoding, just write direct to the OutputStream.
Look at Java: Difference between PrintStream and PrintWriter discussion.
To be quick: you can use -Dfile.encoding=utf8 JVM parameter or method suggested in the discussion (see second answer).