Eclipse 中的 Java 属性 UTF-8 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/863838/
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-08-11 20:24:01  来源:igfitidea点击:

Java properties UTF-8 encoding in Eclipse

javaeclipseencodingutf-8

提问by TJL

I've recently had to switch encoding of webapp I'm working on from ISO-xxto utf8. Everything went smooth, except properties files. I added -Dfile.encoding=UTF-8in eclipse.iniand normal files work fine. Properties however show some strange behaviour.

我最近不得不web应用我的工作从开关编码ISO-xxutf8。一切都很顺利,除了属性文件。我添加-Dfile.encoding=UTF-8eclipse.ini,正常文件工作正常。然而,属性显示出一些奇怪的行为。

If I copy utf8encoded properties from Notepad++ and paste them in Eclipse, they show and work fine. When I reopen properties file, I see some Unicode characters instead of proper ones, like:

如果我utf8从 Notepad++复制编码属性并将它们粘贴到 Eclipse 中,它们会显示并正常工作。当我重新打开属性文件时,我看到一些 Unicode 字符而不是正确的字符,例如:

Zur\u00EF\u00BF\u00BDck instead of Zurück

but app still works fine. If I start to edit properties, add some special characters and save, they display correctly, however they don't work and all previously working special characters don't work any more.

但应用程序仍然可以正常工作。如果我开始编辑属性,添加一些特殊字符并保存,它们会正确显示,但是它们不起作用并且所有以前工作的特殊字符不再起作用。

When I compare local version with CVS I can see special characters correctly on remote file and after update I'm at start again: app works, but Eclipse displays Unicode chars.

当我将本地版本与 CVS 进行比较时,我可以在远程文件上正确地看到特殊字符,更新后我又开始了:应用程序可以工作,但 Eclipse 显示 Unicode 字符。

I tried changing file encoding by right clicking it and selecting ?Other: UTF8” but it didn't help. It also said: ?determined from content: ISO-8859-1”

我尝试通过右键单击它并选择“?其他:UTF8”来更改文件编码,但没有帮助。它还说:?根据内容确定:ISO-8859-1”

I'm using Java 6 and Jboss Developer based on Eclipse 3.3

我正在使用基于 Eclipse 3.3 的 Java 6 和 Jboss Developer

I can live with it by editing properties in Notepad++ and pasting them in Eclipse, but I would be grateful if someone could help me with fixing this in Eclipse.

我可以通过在 Notepad++ 中编辑属性并将它们粘贴到 Eclipse 中来接受它,但如果有人能帮助我在 Eclipse 中解决这个问题,我将不胜感激。

采纳答案by baybora.oren

Don't waste your time, you can use Resource Bundle pluginin Eclipse

不要浪费你的时间,你可以在Eclipse 中使用Resource Bundle 插件

Basic Screen Shot

基本屏幕截图

Old Sourceforge page

旧的 Sourceforge 页面

回答by Jon Skeet

Properties files are ISO-8859-1 by definition - see the docs for the Propertiesclass.

根据定义,属性文件是 ISO-8859-1 - 请参阅Properties类的文档。

Spring has a replacement which can load with a specified encoding, using PropertiesFactoryBean.

Spring 有一个替代品,它可以使用指定的编码加载,使用PropertiesFactoryBean.

EDIT: As Laurence noted in the comments, Java 1.6 introduced overloads for loadand storewhich take a Reader/Writer. This means you can create a reader for the file with whatever encoding you want, and pass it to load. Unfortunately FileReaderstilldoesn't let you specify the encoding in the constructor (aargh) so you'll be stuck with chaining FileInputStreamand InputStreamReadertogether. However, it'll work.

编辑:正如劳伦斯在评论中指出的那样,Java 1.6 引入了重载 forloadstorewhich take a Reader/ Writer。这意味着您可以使用您想要的任何编码为文件创建一个阅读器,并将其传递给load. 不幸的是,FileReader仍然不允许您在构造函数 (aargh) 中指定编码,因此您将陷入链接FileInputStreamInputStreamReader在一起的困境。但是,它会起作用。

For example, to read a file using UTF-8:

例如,要使用 UTF-8 读取文件:

Properties properties = new Properties();
InputStream inputStream = new FileInputStream("path/to/file");
try {
    Reader reader = new InputStreamReader(inputStream, "UTF-8");
    try {
        properties.load(reader);
    } finally {
        reader.close();
    }
} finally {
   inputStream.close();
}

回答by Mario Ortegón

It is not a problem with Eclipse. If you are using the Properties class to read and store the properties file, the class will escape all special characters.

这不是 Eclipse 的问题。如果您使用 Properties 类来读取和存储属性文件,则该类将对所有特殊字符进行转义。

From the class documentation:

从类文档:

When saving properties to a stream or loading them from a stream, the ISO 8859-1 character encoding is used. For characters that cannot be directly represented in this encoding, Unicode escapes are used; however, only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

将属性保存到流或从流加载它们时,将使用 ISO 8859-1 字符编码。对于不能直接用这种编码表示的字符,使用 Unicode 转义;但是,转义序列中只允许有一个“u”字符。native2ascii 工具可用于将属性文件与其他字符编码相互转换。

From the API, store() method:

从 API 中, store() 方法:

Characters less than \u0020 and characters greater than \u007E are written as \uxxxx for the appropriate hexadecimal value xxxx.

对于相应的十六进制值 xxxx,小于 \u0020 的字符和大于 \u007E 的字符写为 \uxxxx。

回答by Alan Moore

There are too many points in the process you describe where errors can occur, so I won't try to guess what you're doing wrong, but I think I know what's happening under the hood.

您描述的过程中有太多可能发生错误的点,所以我不会试图猜测您做错了什么,但我想我知道幕后发生了什么。

EF BF BDis the UTF-8 encoded form of U+FFFD, the standard replacement character that's inserted by decoders when they encounter malformed input. It sounds like your text is being saved as ISO-8859-1, then read as if it were UTF-8, then saved as UTF-8, then converted to the Properties format using native2asciiusing the platform default encoding (e.g., windows-1252).

EF BF BD是 UTF-8 编码形式U+FFFD,解码器在遇到格式错误的输入时插入的标准替换字符。听起来您的文本被保存为 ISO-8859-1,然后像 UTF-8 一样读取,然后保存为 UTF-8,然后native2ascii使用平台默认编码(例如 windows-1252)转换为属性格式)。

ü              => 0xFC                // save as ISO-8859-1
0xFC           => U+FFFD              // read as UTF-8
U+FFFD         => 0xEF 0xBF 0xBD      // save as UTF-8
0xEF 0xBF 0xBD => \u00EF\u00BF\u00BD  // native2ascii

I suggest you leave the "file.encoding" property alone. Like "file.separator" and "line.separator", it's not nearly as useful as you would expect it to be. Instead, get into the habit of always specifying an encoding when reading and writing text files.

我建议您单独保留“file.encoding”属性。像“file.separator”和“line.separator”一样,它并不像你想象的那么有用。相反,养成在读写文本文件时总是指定编码的习惯。

回答by Alan Moore

This seems to work only for some characters ... including special characters for German, Portuguese, French. However, I ran into trouble with Russian, Hindi and Mandarin characters. These are not converted to Properties format 'native2ascii', instead get saved with ?? ?? ??
The only way I could get my app to display these characters correctly is by putting them in the properties file translated to UTF-8 format - as \u0915 instead of ?, or \u044F instead of я. Any advice?

这似乎只适用于某些字符......包括德语、葡萄牙语、法语的特殊字符。但是,我遇到了俄语、印地语和普通话字符的麻烦。这些不会转换为属性格式“native2ascii”,而是使用 ?? ?? ??
让我的应用程序正确显示这些字符的唯一方法是将它们放入转换为 UTF-8 格式的属性文件中 - 作为 \u0915 而不是 ?,或 \u044F 而不是 я。有什么建议吗?

回答by Joaquim Cardeira

Properties props = new Properties();
URL resource = getClass().getClassLoader().getResource("data.properties");         
props.load(new InputStreamReader(resource.openStream(), "UTF8"));

Works like a charm

奇迹般有效

:-)

:-)

回答by Hirantha

Properties props = new Properties();
URL resource = getClass().getClassLoader().getResource("data.properties");         
props.load(new InputStreamReader(resource.openStream(), "UTF8"));

this works well in java 1.6. How can i do this in 1.5, Since Properties class does not have a method to pars InputStreamReader.

这在 java 1.6 中运行良好。我如何在 1.5 中做到这一点,因为 Properties 类没有 pars 的方法InputStreamReader

回答by btpka3

Just another Eclipse plugin for *.properties files:

只是 *.properties 文件的另一个 Eclipse 插件:

Properties Editor

属性编辑器

回答by David Leppik

If the properties are for XML or HTML, it's safest to use XML entities. They're uglier to read, but it means that the properties file can be treated as straight ASCII, so nothing will get mangled.

如果属性用于 XML 或 HTML,则使用 XML 实体是最安全的。它们读起来更难看,但这意味着属性文件可以被视为直接的 ASCII,所以没有任何东西会被破坏。

Note that HTML has entities that XML doesn't, so I keep it safe by using straight XML: http://www.w3.org/TR/html4/sgml/entities.html

请注意,HTML 具有 XML 没有的实体,因此我通过使用直接的 XML 来保证它的安全:http: //www.w3.org/TR/html4/sgml/entities.html

回答by htobon

I recommend you to use Attesoro (http://attesoro.org/). Is simple and easy to use. And is made in java.

我建议您使用 Attesoro ( http://attesoro.org/)。简单易用。并且是用java制作的。