Java properties UTF-8 encoding in Eclipse

2020-01-23 15:32发布

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

If I copy utf8 encoded 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:

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.

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”

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

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.

12条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-01-23 16:02

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?

查看更多
虎瘦雄心在
3楼-- · 2020-01-23 16:03

You can define UTF-8 .properties files to store your translations and use ResourceBundle, to get values. To avoid problems you can change encoding:

String value = RESOURCE_BUNDLE.getString(key); 
return new String(value.getBytes("ISO-8859-1"), "UTF-8");
查看更多
Juvenile、少年°
4楼-- · 2020-01-23 16:04

Just another Eclipse plugin for *.properties files:

Properties Editor

查看更多
再贱就再见
5楼-- · 2020-01-23 16:06

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

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

EDIT: As Laurence noted in the comments, Java 1.6 introduced overloads for load and store which 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 FileReader still doesn't let you specify the encoding in the constructor (aargh) so you'll be stuck with chaining FileInputStream and InputStreamReader together. However, it'll work.

For example, to read a file using 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();
}
查看更多
贼婆χ
6楼-- · 2020-01-23 16:07

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

查看更多
我命由我不由天
7楼-- · 2020-01-23 16:09

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

Basic Screen Shot

Old Sourceforge page

查看更多
登录 后发表回答