How to solve the problem of garbled characters in Java read properties file

Source: Internet
Author: User

The Java Read properties file contains two main types of Chinese characters:

1.key contains Chinese characters (which may also be included in value)

2.key does not contain Chinese characters (it is possible to include in value)

1. Key contains Chinese characters

You can use the Java native2ascii.exe tool (Java\jdk1.x.x\bin\native2ascii.exe) to convert the file encoding format

Example: native2ascii-encoding 8859_1 c:\a.properties c:\b.properties

The C:\a.properties file contents will be 8859_1 transcoded, saved as c:\b.properties file.

Command format: Native2ascii-[options] [inputfile [OutputFile]]

Parameter option Options-reverse: Convert Latin-1 or Unicode encoding to local encoding-encoding encoding_name: Specify the encoding to use when converting Inputfile: file to convert outputfile: converted File
Reciprocal (-encoding, non-English content (such as Chinese) to convert between coded or encoded characters)
Reversal (-reverse, usually converting a coded character to a non-English content, or a conversion between non-English content)
The file encoding and local encoding to be reversed should be consistent:
Example: After converting Chinese to ISO 8859_1 encoding, convert the 8859_1 code to Chinese:
Native2ascii-encoding 8859_1 C:\a.txt C:\b.txt,
Transcode A with 8859_1 and save as B (8859_1 code)
Native2ascii-encoding GBK C:\b.txt C:\c.txt,

Transcode B with GBK and Save as C (GBK code)


Native2ascii-reverse C:\c.txt C:\d.txt,
GBK encoding c with local code transcoding, save as D (Chinese content) Chinese to GBK encoding, the GBK code to Chinese:

Native2ascii-encoding GBK C:\a.txt C:\b.txt,
Transcode A with GBK and save as B (GBK code)

Native2ascii-reverse C:\b.txt C:\c.txt,
GBK encoded B with local code transcoding, save as C (Chinese content)

After doing this, read the key contains the Chinese properties, you need to have the following conversion code:

String value = "China";
String value2 = new String (Value.getbytes (), "iso-8859-1");
System.out.println (P.getproperty (value2));

2.key does not contain Chinese characters, in value

Direct use of 1 in:

String value = P.getproperty (key);
String value2 = new String (Value.getbytes ("iso-8859-1"), "GBK");

How to solve the problem of garbled characters in Java read properties file

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.