如何让eclipse在unicode中打印出奇怪的字符? [英] How do I make eclipse print out weird characters in unicode?

查看:143
本文介绍了如何让eclipse在unicode中打印出奇怪的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让我的程序输出一个带有名单列表的文本文件。有些名字有奇怪的字符,比如Åström。

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.

我从一个以UTF-8编码的网页上抓住了这些名字列表,或者至少我很确定它确实如此,因为页面来源说

I have grabbed these list of names from a webpage that is encoded in "UTF-8", or at least I'm pretty sure it does because the page source says


meta http-equiv =Content-Typecontent =text / HTML; charset = UTF-8/

" meta http-equiv="Content-Type" content="text/html; charset=UTF-8" / "

这是我到目前为止所尝试的:

This is what I've tried so far:

public static void write(List<String> list) throws IOException  {
        Writer out = new OutputStreamWriter(new FileOutputStream("test.txt"), "UTF-8");
        try {
            for (int i=0;i<list.size();i++) {
                try {
                    byte[] utf8Bytes = list.get(i).getBytes("UTF-8");
                    out.write(new String(utf8Bytes, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                out.write(System.getProperty("line.separator"));

            }
        }
        finally {
        out.close();
        }
    }

我有点困惑为什么它是不工作我得到的输出是Ã...ström,这很奇怪。

and I'm a little confused as to why it's not working. The output I get is "Åström", which is very weird.

有人可以指出我正确的方向吗?谢谢!

Can someone please point me in the right direction? Thanks!

在另一个不相关的说明中,除了笨重的

And on another unrelated note, is there an easier way to write a new line to a text file besides the clunky


out.write(System.getProperty(line.separator));

out.write(System.getProperty("line.separator"));

我有吗?我在网上看到了它,但它确实有用,但我只是想知道是否有更清洁的方式。

that I have? I saw that online somewhere and it works, but I was just wondering if there was a cleaner way.

推荐答案

设置你的 Eclipse>偏好>一般>工作区>文本文件编码为UTF-8。

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

这篇关于如何让eclipse在unicode中打印出奇怪的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆