Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java properties UTF-8 encoding in Eclipse

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.

like image 557
TJL Avatar asked May 14 '09 14:05

TJL


People also ask

How do I change the encoding to UTF-8 in Eclipse?

Open Eclipse and do the following steps: Window -> Preferences -> Expand General and click Workspace, text file encoding (near bottom) has an encoding chooser. Select "Other" radio button -> Select UTF-8 from the drop down. Click Apply and OK button OR click simply OK button.

How do I change the encoding of a properties file in Eclipse?

properties files are Latin1 (ISO-8859-1) encoded by definition. ISO-8859-1 as its default encoding. You can change this under: Preferences > General > Content Types.

How do I change the default encoding in Eclipse?

In Eclipse, go to Preferences>General>Workspace and select UTF-8 as the Text File Encoding. This should set the encoding for all the resources in your workspace. Any components you create from now on using the default encoding should all match.


1 Answers

Answer for "pre-Java-9" is below. As of Java 9, properties files are saved and loaded in UTF-8 by default, but falling back to ISO-8859-1 if an invalid UTF-8 byte sequence is detected. See the Java 9 release notes for details.


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(); } 
like image 146
Jon Skeet Avatar answered Sep 20 '22 17:09

Jon Skeet