如何使PrintWriter编写UTF-8?

2022-08-31 21:00:22

如何编写 UTF-8?PrintWriter

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

答案 1

使用 :OutputStreamWriter

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

答案 2
OutputStream os = new FileOutputStream("file.txt");
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

推荐