java PrintWriter 不会以 UTF-8 写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26334988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:44:11  来源:igfitidea点击:

PrintWriter won't write in UTF-8

javaprintwriter

提问by Kreuzbube

My code is

我的代码是

    PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile),
                StandardCharsets.UTF_8), true);
output.print(SomeString);

but if i run this I Still have Problems wit ???ü and so on. is there someone how can explain me this?

但如果我运行这个,我仍然有问题 ???ü 等等。有没有人可以向我解释这个?

If i do

如果我做

System.out.print(someString);

it prints out perfect with ??ü? thanks for helping

它打印出来很完美??ü?谢谢你的帮助

回答by Divya

res.setContentType("text/html; charset=UTF-8");
PrintWriter out = new PrintWriter(
                         new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);

also you can check encoding type

您也可以检查编码类型

String encoding = request.getCharacterEncoding();
            if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
            {
                response.setContentType("text/html; charset=utf-8");
            }