Java Properties garbled Problem Solution

Source: Internet
Author: User

Properties is a class provided by java to solve key-value pairs stored in files. It inherits HashTable and can process java standard configuration files.

However, when Properties reads text from a file stream, getProperty (key) is decoded using a ISO8859-1, so garbled characters are read from Chinese, the read string needs to be encoded back from the ISO8859-1 and decoded in the text encoding format.
String s = new String (properties. getProterty (key). getBytes [] ("ISO8859-1"), "UTF-8 ");
UTF-8 is its own encoding format.

Note: In Properties. load
Java code
FileInputStream isr = new FileInputStream (savePath );
Properties props = new Properties ();
Props. load (isr );

Note: isr must be a file byte stream, not a volume stream. Otherwise, garbled characters may occur. (Or there is a solution, but I haven't found it yet !)
Java code
OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream (savePath), "UTF-8 ");
Props. store (osw, "This is the System Config file, Please don't delete or modify it! ");

Author "wangboak"
 

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.