Chinese garbled solution when Java reads the properties configuration file

Source: Internet
Author: User

Chinese garbled solution when Java reads the properties configuration file

Transferred from: http://pig345.iteye.com/blog/725974


Encountered the use of Java.util.Properties reading Chinese content (UTF-8 format) of the configuration file, the occurrence of Chinese garbled phenomenon,

Java code
  1. properties prop=new properties ();         
  2. Prop.load (Client.   Class. getClassLoader (). getResourceAsStream ("config.properties"));

Habitually Google a bit, most of the online article is to let everyone use native2ascii.exe conversion Such solution, at first, almost be confused, thought can only use such mealy method ...

But the point is, it's too round! If every time with Native2ascii.exe to convert Chinese into \uxxxx\uxxxx such, trouble first not to say, after the conversion of the file is completely unreadable!!! This is basically intolerable!
(although it can also be converted back with Native2ascii.exe, but again, trouble!) )

Calm down, suddenly remembered, or beginner Java saw, java.io package in the difference between Reader/writer and stream.
(years old, the details forget, presumably:reader/write is processing encoded text, and Inputstream/outputstream only the data as 2 binary stream)

The right solution

Java code
  1. properties prop=new properties ();         
  2. Prop.load (new InputStreamReader (Client).          Class. getClassLoader (). getResourceAsStream ("Config.properties"), "UTF-8"));

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.