Encoding of properties files in eclipse

Source: Internet
Author: User

From: http://blog.csdn.net/uestcong/article/details/6635123

1. Modify eclipse settings

The configuration file is used in the project, so a new. properties file is created in eclipse. The file is edited in Chinese and the following error is reported during saving:

Solution:

Select in sequence:

Menu --> preferences --> General --> contenttypes --> text --> JAVA properties file, set default encoding, change the following ISO-8859-1 to UTF-8 or GBK (recommended UTF-8), then update, as shown in.


2. format conversion in Java code

After the above steps, the file can be saved in UTF-8 format, but then there is a problem.

The Code is as follows:

[Java]View
Plaincopy

  1. // Load from the configuration file
  2. Configuration cfg = new Configuration ("etc/orgInfo. properties ");
  3. String _ orgName = cfg. getValue ("ORGNAME ");
  4. System. out. println ("before format change" + _ orgName );

The result is as follows: change the format of the prefix "çμ ç ç§ æ å ¤ *************************

It is certain that the file is converted to another format after being read, but the reason has not been found yet.

Therefore, the following changes were made:

[Java]View
Plaincopy

  1. Configuration cfg = new Configuration ("etc/orgInfo. properties ");
  2. String _ orgName = cfg. getValue ("ORGNAME ");
  3. System. out. println ("before format change" + _ orgName );
  4. // The encoding format must be converted. Otherwise, garbled characters will occur.
  5. String cn_orgName = new String (_ orgName. getBytes ("ISO-8859-1"), "UTF-8 ");
  6. OrgName. setAttribute ("value", cn_orgName );

The output result is as follows:

Change the format of the prefix "C. ç §?"?
E-Science and Technology College Hospital after the format change

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.