Search code examples
javaprintwriter

PrintWriter won't write in UTF-8


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


Solution

  • 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");
                }