How do I make eclipse print out weird characters in unicode?

前端 未结 3 1540
情深已故
情深已故 2021-02-09 07:41

So I\'m trying to make my program output a text file with a list of names. Some of the names have weird characters, such as Åström.

I have grabbed these list of names fr

相关标签:
3条回答
  • 2021-02-09 07:58

    Notepad is not a particularly feature rich editor. It will attempt to guess the document encoding, sometimes with unexpected results. "Plain text" documents don't carry any metadata about their encoding which gives them certain limitations. Windows apps (Notepad included) often rely on the byte-order-mark (U+FEFF or "\uFEFF" in Java strings) to determine if the encoding is a Unicode format. That might help out Notepad; it's going to be useless for your web page problem.

    The HTML 4 spec defines how output encoding should be set. You should set the Content-Type HTTP header in addition to specifying the meta encoding.

    You don't mention what you're using in your web app. A servlet should set the content type setContentType("text/html; charset=UTF-8"); a JSP should use the page directive to do the same. Other view technologies will provide similar mechanisms.


    byte[] utf8Bytes = list.get(i).getBytes("UTF-8");
    out.write(new String(utf8Bytes, "UTF-8"));
    

    This code performs some useless operations; it transcodes character data from UTF-16 to UTF-8, then back from UTF-8 to UTF-16, then writes data to a Writer (which will transcode the UTF-16 to UTF-8 again). This code is equivalent:

    String str = list.get(i);
    out.write(str);
    

    Use a PrintWriter to get newline support.


    You can read more about character encoding in Java here, here and here.

    0 讨论(0)
  • 2021-02-09 08:04

    The content is indeed in UTF-8 and it appears OK if printed to the console. What may be causing the problem is the decoding and encoding of the string which is unnecessary. Instead of an OutputStreamWriter try using a java.io.PrintWriter. It has the println methods that print out the string with the system line separator at the end. It would look something like:

    printStream.println(list.get(i));
    

    Also, when opening the file to see it try using a browser. They allow you to choose the encoding after opening it so you can try several encodings quickly to see what is being really used.

    0 讨论(0)
  • 2021-02-09 08:08

    Set your Eclipse > Preferences > General > Workspace > Text file encoding to UTF-8.

    0 讨论(0)
提交回复
热议问题